This worked for my example data frame but not when I used it on my actual data frame because some of the levels of x have only one element. However, when I replace 'sample' with 'some' (from the car package), it works like a charm!
Many thanks to Andy and Gabor for their help! Kelly Liaw, Andy wrote: > The following should work: > > >>dfr.samp <- dfr[tapply(1:nrow(dfr), dfr$x, sample, 1),] >>dfr.samp > > x y z > 10 a 10 J > 2 b 2 B > 9 c 9 I > > Andy > > From: Kelly Hildner > >>I don't use R much, and I have been unable to figure out how >>to get the >>subset of my data frame that I would like. >> >>For example, if this were my data frame: >> >> > dfr <- data.frame(x=rep(letters[1:3], 4), y=(1:12), >>z=(LETTERS[1:12])) > dfr >> x y z >>1 a 1 A >>2 b 2 B >>3 c 3 C >>4 a 4 D >>5 b 5 E >>6 c 6 F >>7 a 7 G >>8 b 8 H >>9 c 9 I >>10 a 10 J >>11 b 11 K >>12 c 12 L >> >>I would like to randomly select one row for each level of the >>factor x >>and create a new data frame with the results. For example, >>the result >>might be: >> >> x y z >>1 a 1 A >>5 b 5 E >>6 c 6 F >> >>Any help would be greatly appreciated! >> >>Thanks, >>Kelly >> >>-- >>K. Kelly Hildner, Ph.D. >>NOAA Fisheries >>Southwest Fisheries Science Center >>110 Shaffer Rd. >>Santa Cruz, CA 95060 >> >>______________________________________________ >>[email protected] mailing list >>https://stat.ethz.ch/mailman/listinfo/r-help >>PLEASE do read the posting guide! >>http://www.R-project.org/posting-guide.html >> >> > > > > ------------------------------------------------------------------------------ > Notice: This e-mail message, together with any attachment...{{dropped}} ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
