Okay, first off, your code is unnecessarily complicated. Check out several simplifications below that will hopefully make things simpler.
Then, to your core question, you want a split-apply-combine strategy. You need to split the data by the relevant grouping variables, then do a t.test
in each subset of the data. You can achieve this with a combination of split
and lapply
. (Per your comment, to obtain results for multiple outcomes, you need nested lapply
functions).
# the data, again
data <- iris
data$location <- factor(rep(c("US", "USSR"), length.out = length(data$Sepal.Length)))
data$subgroup <- factor(rep(c(rep("A", 25), rep("B", 25)), 3))
somedata <- data[data$Species %in% c("setosa","versicolor"),]
DVs <- c('Sepal.Width','Sepal.Length','Petal.Length')
out <- lapply(DVs, function(x){
lapply(split(somedata, list(somedata$location, somedata$subgroup)),
function(z) {
t.test(update(~ Species,paste(x,'~.')), data=z)$p.value
})
})
Here's the result:
> do.call(cbind, setNames(out,DVs))
Sepal.Width Sepal.Length Petal.Length
US.A 9.183405e-05 9.371858e-06 5.852323e-14
USSR.A 0.0001211233 0.0001385488 2.97461e-12
US.B 8.473525e-06 0.0001902751 4.123647e-11
USSR.B 0.001818272 5.308597e-06 7.593105e-11