mardi 18 octobre 2016

Randomly return a row number for a subset in the data frame

I'd like to be able to randomly return a row number from a data set, where the rows are a subset of the data. For example with the dataframe

x.f<-data.frame(G=c("M","M","M","M","M","M","F","F","F","F","F","F"),A=c("1","2","3","1","2","3","1","2","3","1","2","3"),E=c("W","W","W","B","B","B","W","W","W","B","B","B"))

I'd like to, say, randomly give me a row number where G=="M" and A=="3", so the answer will be row 3 or row 6. The number returned must be the position in the original data frame. Whilst this example is nicely structured (each possible combination appears once only), in reality there will not be such a structure, eg the combination (M,2,W) will be randomly distributed throughout the data frame and can occur more or fewer times than other combinations.




Aucun commentaire:

Enregistrer un commentaire