Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这行代码rep(c(0), 2) 创建
rep(c(0), 2)
x 1 0 2 0
我想以某种适合 R 的方式扩展它,以便最终得到类似于下面的向量的东西。基本上,我想成对附加整数:
x 1 0 2 0 3 1 4 1 5 2 6 2
我觉得这是 R 不喜欢循环的一个例子,它有意想不到的副作用,让简单的事情看起来不熟悉,让我渴望其他语言的其他工具,比如 Python 中的 numpy。
我可以以这种方式迭代和递增的最佳方式是什么?
复制 Nishanth 的答案,这正是您想要的。
rep(0:2, each=2)
data.frame(x = unlist(lapply(0:2, rep, 2)))