[R] sampling one random frame from each unique trial?

Daniel Malter daniel at umd.edu
Mon Jun 28 05:56:24 CEST 2010


Hi, take the following example and proceed accordingly.

Name=c("Miller","Miller","Miller","Miller","Smith","Smith","Smith","Smith")
X=rnorm(8)
Year=rep(2000:2003,2)

d=data.frame(Name,X,Year)

#Row indices
rows=1:dim(d)[1]

#Which Name occupies which rows?
#"Name" would be your "file"
w=function(x){which(Name%in%unique(x))}
samplefrom=tapply(Name,Name,w)

#Sample one row index for each Name and
#give the data frame d for these row indices
f=function(x){sample(x,1)}
d[unlist(lapply(samplefrom,f)),]

HTH,
Daniel

-- 
View this message in context: http://r.789695.n4.nabble.com/sampling-one-random-frame-from-each-unique-trial-tp2270396p2270465.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list