[R] extracting a percentage of data by random

jim holtman jholtman at gmail.com
Thu Mar 6 01:57:25 CET 2008


Here is one way of doing it:

> x <- data.frame(group=sample(1:4,100,TRUE), age=runif(100,4,80))
> tapply(x$age, x$group, function(z) mean(z[sample(seq_along(z), length(z) / 10)]))
       1        2        3        4
34.56628 58.70901 54.26239 58.89306
>
>


On Wed, Mar 5, 2008 at 7:49 PM, Chang Liu <changisme at hotmail.com> wrote:
>
> Hello Gurus:
>
> If I have a dataframe with one of the variables called "age" for example, and I want to extract a random 10% of the observations from each "age" group of the entire data frame. Do I have to double loop to split the data and then loop again to assign random numbers? Or is there a better way to do this?
>
> Thanks!
> Karen
>
>
>
> _________________________________________________________________
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list