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 --
How to resample one per group
2 messages · Johannes Radinger, ONKELINX, Thierry
Something like this?
library(plyr)
ddply(df, .(group), function(x){
x[sample(nrow(x), 1), ]
})
Best regards,
Thierry
----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium
Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
-----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Johannes Radinger Verzonden: donderdag 17 november 2011 12:37 Aan: r-help at r-project.org Onderwerp: [R] How to resample one per group 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 --
______________________________________________ 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.