首页 > 解决方案 > 使用第 12、24、36、...、288 行的值创建一个数据框

问题描述

我有一个 288 行 4 列的数据框。我只需要第12、24、36、...、288行的值,也就是12的规则间隔,我该怎么做呢?

标签: rdataframerowsreshape

解决方案


您可以使用包中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 日创建


推荐阅读