문제

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