[R] How to resample one per group

Johannes Radinger JRadinger at gmx.at
Thu Nov 17 12:36:59 CET 2011


Hello,

I have got a dataframe which looks like:

y <- c(1,5,6,2,5,10) # response
x <- c(2,12,8,1,16,17) # predictor
group <- factor(c(1,2,2,3,4,4)) # group
df <- data.frame(y,x,group)

Now I'd like to resample that dataset. 
I want to get dataset (row)
per group. So per total sample I get 4 rows
into a new data frame. How can I do that?
Is there any simple approach using an existing
package.

I looked at function strata() from package sampling.
I don't if that is the function for that or
if there is a simpler approach with sample().

What I unsuccessfully tried so far:

library(sampling)
strata(data=df,group,size=(rep(1,nlevels(group))))

Maybe you can help me to do this resampling...

Thank you,
Johannes
--



More information about the R-help mailing list