[R] Randomly selecting one row for each factor level [Broadca st]
Liaw, Andy
andy_liaw at merck.com
Thu Apr 20 12:48:29 CEST 2006
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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
More information about the R-help
mailing list