R:在一次调用中生成多个编号规则 [duplicate]

R: generate many number sequences in one call [duplicate]

提问人:jeanlain 提问时间:8/10/2015 最后编辑:ASTjeanlain 更新时间:8/11/2015 访问量:76

问:

我有 2 个整数值向量。

> starts = 1:10
> ends = seq(1,100,10)
> ends
 [1]  1 11 21 31 41 51 61 71 81 91

我想从头到尾生成数字序列,以便从 1 到 1、2 到 11、3 到 21 等有 10 个序列。

我可以很容易地用for()循环来做到这一点,因为我认为会有更优雅、更快的东西。 似乎没有这样做,因为它抱怨:do.call()

> x = do.call(seq,list(from=starts,to=ends,by=1))
Error in seq.default(from = 1:10, to = c(1, 11, 21, 31, 41, 51, 61, 71,  : 
  'from' must be of length 1

最好的方法是什么?

r do.call

评论


答:

2赞 akrun 8/10/2015 #1

你可以试试Map

 Map(`:`, starts, ends)

它将为 'starts', 'ends' 的相应元素生成序列。list

或者,我们可以用(如前所述@MrFlick) 代替:seq

 Map(seq, starts, ends)

评论

0赞 MrFlick 8/10/2015
或等效Map(seq, starts, ends)