seq_along() - 截断r中的复制

Ale*_*lex 1 r dplyr

我想生成月份编号以及值列表.问题是该列表不是12个月的完整2次重复.第一年为12年,第二年为10年.

tibble(value=rnorm(22))
Run Code Online (Sandbox Code Playgroud)

我尝试过的一些事情是rep(1:12,2),当它到达数据帧长度的末尾时,会认为序列会停止.我也尝试seq_along(along.with=value,1:12)过同样的思路.

Rei*_*son 9

你想要length.out参数rep():

rep(1:12, length.out = 22)
Run Code Online (Sandbox Code Playgroud)

这使

> rep(1:12, length.out = 22)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10
Run Code Online (Sandbox Code Playgroud)

我们得到这个是因为,来自?rep:

 ‘length.out’ may be given in place of ‘times’, in which case ‘x’
 is repeated as many times as is necessary to create a vector of
 this length.  If both are given, ‘length.out’ takes priority and
 ‘times’ is ignored.
Run Code Online (Sandbox Code Playgroud)