Hi R experts! I would like to have a scripted solution that will iteratively subset data across many variables per factor level of each variable.
To illustrate, if I create a dataframe (df) by: variation <- c("A","B","C","D") element1 <- as.factor(c(0,1,0,1)) element2 <- as.factor(c(0,0,1,1)) response <- c(4,2,6,2) df <- data.frame(variation,element1,element2,response) I would like a function that would allow me to subset the data into four groups and perform analysis across the groups. One group for each of the two factor levels across two variables. In this example its fairly easy because I only have two variables with two levels each, but would I would like this to be extendable across situations where I am dealing with more than 2 variables and/or more than two factor levels per variable. I am looking for a result that will mimic the output of the following: element1_level0 <- subset(df,df$element1=="0") element1_level1 <- subset(df,df$element1=="1") element2_level0 <- subset(df,df$element2=="0") element2_level1 <- subset(df,df$element2=="1") The purpose would be to perform analysis on the df across each subset. Simplistically this could be represented as follows: mean(element1_level0$response) mean(element1_level1$response) mean(element2_level0$response) mean(element2_level1$response) Thanks, Reid [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.