Random row selection in R

r

Solution

I think you can do this with the `plyr` package:

library("plyr")
ddply(df,.(name),randomRows,1)

which gives you for example:

  id name value
1  1    A     8
2  2    B    11
3  3    C    12

Is this what you are looking for?

Problem

I have this dataframe ``` id <- c(1,1,1,2,2,3) name <- c("A","A","A","B","B","C") value <- c(7:12) df<- data.frame(id=id, name=name, value=value) df ``` This function selects a random row from it: ``` randomRows = function(df,n){ return(df[sample(nrow(df),n),]) } ``` i.e. ``` randomRows(df,1) ``` But I want to randomly select one row per 'name' (or per 'id' which is the same) and concatenate that entire row into a new table, so in this case, three rows. This has to loop throught a 2000+ rows dataframe. Please show me how?!

Original source