Sampling small data frame from a big dataframe

后端 未结 2 1460
野趣味
野趣味 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.

提交回复
热议问题