Lucas
Lucas

Reputation: 589

How to repeat values of a vector based on its own value in r

I want to create a vector1 that consist of the repetition of each value of another vector2 as many times as the value itself of each element of vector2

V2 <- c(1,3,2)

V1 should be:

c(1,3,3,3,2,2)

Upvotes: 2

Views: 96

Answers (1)

akrun
akrun

Reputation: 887128

We can use rep to do this

rep(V2, V2)
#[1] 1 3 3 3 2 2

Upvotes: 4

Related Questions