[R] Select values at random by id value

hadley wickham h.wickham at gmail.com
Wed Jul 1 21:38:39 CEST 2009


On Wed, Jul 1, 2009 at 2:10 PM, Sunil
Suchindran<sunilsuchindran at gmail.com> wrote:
> #Highlight the text below (without the header)
> # read the data in from clipboard
>
> df <- do.call(data.frame, scan("clipboard", what=list(id=0,
> date="",loctype=0 ,haptype=0)))
>
> # split the data by date, sample 1 observation from each split, and rbind
>
> sampled_df <- do.call(rbind, lapply(split(df,
> df$date),function(x)x[sample(1:nrow(x), 1),]))

ddply from the plyr package (http://had.co.nz/plyr), makes this sort
of operation a little simpler:

ddply(df, "date", function(df) df[sample(nrow(df), 1), ])

Hadley


-- 
http://had.co.nz/




More information about the R-help mailing list