r - 使用第 12、24、36、...、288 行的值创建一个数据框
问题描述
我有一个 288 行 4 列的数据框。我只需要第12、24、36、...、288行的值,也就是12的规则间隔,我该怎么做呢?
解决方案
您可以使用包中slice()
的功能dplyr
PS:下次请提供数据和代码的可重现问题,以便其他人可以提供帮助。在此处查看更多信息:如何制作出色的 R 可重现示例?
library(tibble)
library(dplyr)
chosen_vect <- seq(12, 120, 12)
chosen_vect
#> [1] 12 24 36 48 60 72 84 96 108 120
iris %>%
rownames_to_column() %>% # to test if we slice the right rows
slice(chosen_vect)
#> rowname Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 12 4.8 3.4 1.6 0.2 setosa
#> 2 24 5.1 3.3 1.7 0.5 setosa
#> 3 36 5.0 3.2 1.2 0.2 setosa
#> 4 48 4.6 3.2 1.4 0.2 setosa
#> 5 60 5.2 2.7 3.9 1.4 versicolor
#> 6 72 6.1 2.8 4.0 1.3 versicolor
#> 7 84 6.0 2.7 5.1 1.6 versicolor
#> 8 96 5.7 3.0 4.2 1.2 versicolor
#> 9 108 7.3 2.9 6.3 1.8 virginica
#> 10 120 6.0 2.2 5.0 1.5 virginica
由reprex 包(v0.2.1.9000)于 2018 年 10 月 31 日创建
推荐阅读
- r - 使用 dplyr 使用不同列的值向列名添加前缀
- java - 如何将 InputStream 转换为 FilePart?
- django-rest-framework - 不能在 DRF 中使用全文搜索
- javascript - window.opener 在 Chrome 中返回 Cross Origin 错误
- xml - 创建嵌套 XML 节点
- python - Python如何正确打印cp1258越南语字符?
- python - 使用 python 中的数据更新 Prior - Beta 和正态分布
- r - 计算R中两个数据帧之间的新朋友和丢失朋友的数量
- node.js - 使用 -Node.js 使用 Selenium 处理浏览器通知
- c# - ContinueWith 和 without 有什么区别?