If I want to randomly select some samples from different groups I use the plyr package and the code below
require(plyr)
sampleGroup<-function(df,size) {
df[sample(nrow(df),size=size),]
}
iris.sample<-ddply(iris,.(Species),function(df) sampleGroup(df,10))
Here 10 samples are selected from each species.
Some of my dataframes are very big and my question is can I use the same sampleGroup function with the dplyr package? Or is there another way to do the same in dplyr?
EDIT
Version 0.2 of the dplyr package introduced two new functions to select random rows from a table sample_n and sample_frac
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…