giannisgr
giannisgr

Reputation: 23

Replicate rows of dataframe

df =data.frame ("x"=c(5,4,10,7) , "y"=c (rep (1,2),rep (2,2))

I'm trying to replicate each x y times and then save it to a variable so the result will be like this:

a=c (5,4)

then

a=c (10,10,7,7)

Probably it is an easy one, but I'm new to programming..thanks in advance

Upvotes: 1

Views: 112

Answers (1)

Clemsang
Clemsang

Reputation: 5481

You can use the split function that creates a list of length distincts values of y :

split(rep(df$x, df$y), rep(df$y, df$y)) 

$`1`
[1] 5 4

$`2`
[1] 10 10  7  7

Upvotes: 1

Related Questions