Question

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?

Était-ce utile?

La solution

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

rep(0:2, each=2)

Autres conseils

data.frame(x = unlist(lapply(0:2, rep, 2)))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top