Re: [R] extracting a percentage of data by random

2008-03-05 Thread jim holtman
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)])) 1234 34.56628 58.70901 54.26239 58.89306 On Wed, Mar 5, 2008 at 7:49 PM, Chang Liu

Re: [R] extracting a percentage of data by random

2008-03-05 Thread Bill.Venables
You don't need any explicit loops at all. Here is a demo of one way to do it: set.seed(23) # on Windows dat - data.frame(age = factor(sample(1:4, 200, rep = T)), y = runif(200)) head(dat) # ages are in random order age y 1 3 0.64275524 2 1 0.56125314 3 2 0.82418228 4

Re: [R] extracting a percentage of data by random

2008-03-05 Thread Chang Liu
Thank you! That was very helpful indeed! Karen Subject: RE: [R] extracting a percentage of data by random Date: Thu, 6 Mar 2008 11:20:16 +1000 From: [EMAIL PROTECTED] To: [EMAIL PROTECTED]; r-help@r-project.org You don't need any explicit loops at all. Here is a demo of one way to do it: