Quantcast
Channel: StackExchange Replication Questions
Viewing all articles
Browse latest Browse all 17268

Replicating a data.frame according to a vector of lengths`

$
0
0

I have a data.frame:

set.seed(1)
short.df <- data.frame(id=letters[1:10],name=LETTERS[1:10])

And I want to replicate each row by a number of times given by a vector whose length equals nrow(short.df):

lengths <- c(sample(10000,10,replace=F))

This takes too long for my real data size:

long.df <- do.call(rbind,lapply(1:length(lengths),function(x) data.frame(id=rep(short.df$id,lengths[x]),name=rep(short.df$name,lengths[x]))))

Any way to do it faster?


Viewing all articles
Browse latest Browse all 17268

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>