samedi 26 novembre 2016

R conditional random samples

How can I get a random samples based on conditional values. For example I have the following dataframe:

GROUP CLASS  AGE
A     1      10
A     2      15
B     1      10
B     2      17
C     1      12
C     2      14

I need to get a sample of 30 records for each of the GROUPS, but only from CLASS = 1 compiled all in a sample dateframe.

I Know how to get a sample of 30 records, but I don't know how to create a condition that loops throught the different GROUPS and filters the CLASS

ran.sample = sample(nrow(df_all), 30)
df = df_all[ran.sample, ]

Any ideas?

Thanks




Aucun commentaire:

Enregistrer un commentaire