Sampling small data frame from a big dataframe

这一生的挚爱 提交于 2019-12-01 20:37:35

I think what you want is to subset the data frame passed in x using sample:

ddply(data1,.(a),function(x) x[sample(nrow(x),20,replace = FALSE),])

But, of course, you still need to take care that the size of the sample for each piece (in this case 20) is at least as big as the smallest subset of your data based on the levels of a.

It would seem that if you want to sample a category that has less than 20 rows, you'd need replace=TRUE...

This might do the trick:

ddply(data1,'a',function(x) x[sample.int(NROW(x),20,replace=TRUE),])
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!