mardi 14 juillet 2020

Sample random rows

I have a data set in which there are multiple ID's, which have two observations/values each, e.g:

 ID           Test   Behaviour     
 A12.4.2      ONE     8.64      
 A12.4.2      TWO     7.63      
 A6.3.3       ONE     1.81      
 A6.3.3       TWO     2.47      
 B12.4.1      ONE     1.17      
 B12.4.1      TWO     3.96      
 E9.4.2       ONE     13.04     
 E9.4.2       TWO     9.51

I have found that to randomly sample rows to retain data on both ID's and the observation data associated with those ID's that I can use:

df[sample(nrow(df), 32), ]

However, I am struggling to find a function that will ensure that the two measures for each ID are included within the random sample. In the end, I would like a random sample made up of 16 ID's with two observations each, totaling 32 observations. Can anyone help?




Aucun commentaire:

Enregistrer un commentaire