[R] Iteratively subsetting data by factor level across multiple variables
Reid Bryant
reidbryant at gmail.com
Thu Jan 15 20:42:33 CET 2015
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]]
More information about the R-help
mailing list