Sampling small data frame from a big dataframe

后端 未结 2 1427
野趣味
野趣味 2021-01-20 06:15

I am trying to sample a data frame from a given data frame such that there are enough samples from each of the levels of a variable. This can be achieved by separating the d

相关标签:
2条回答
  • 2021-01-20 06:57

    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.

    0 讨论(0)
  • 2021-01-20 06:57

    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),])
    
    0 讨论(0)
提交回复
热议问题