R rep seq where number of rows is not multiple of seq length

家住魔仙堡 提交于 2019-12-01 01:32:38

Something, like this?

df <- data.frame(rnorm(1666))
df$cutter <- rep(1:5, length.out=1666)

tail(df)
     rnorm.1666. cutter
1661  0.11693169      1
1662 -1.12508091      2
1663  0.25441847      3
1664 -0.06045037      4
1665 -0.17242921      5
1666 -0.85366242      1

Use the length.out argument of rep().

Here is an example using the built-in dataset cars.

str(cars)
'data.frame':   50 obs. of  2 variables:
 $ speed: num  4 4 7 7 8 9 10 10 10 11 ...
 $ dist : num  2 10 4 22 16 10 18 26 34 17 ...

Add grouping column:

cars$group <- rep(1:3, length.out=50)

Inspect the result:

head(cars)
  speed dist group
1     4    2     1
2     4   10     2
3     7    4     3
4     7   22     1
5     8   16     2
6     9   10     3

tail(cars)
   speed dist group
45    23   54     3
46    24   70     1
47    24   92     2
48    24   93     3
49    24  120     1
50    25   85     2
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!