This line of code rep(c(0), 2) creates

    x
1   0
2   0

I would like to somehow extend this, in a way appropriate for R, so that I end up with something like the vector below. Basically, I'd like to append integers pairwise as such:

    x
1   0
2   0
3   1
4   1
5   2
6   2

I feel like this is an example of R's dislike of loops which has the unintended side effect of makes simple things seem unfamiliar and makes me long for other tools in other languages, like perhaps numpy in Python.

What is the best way I can iterate and increment in this manner?

有帮助吗?

解决方案

Copying Nishanth's answer, this does exactly what you want.

rep(0:2, each=2)

其他提示

data.frame(x = unlist(lapply(0:2, rep, 2)))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top