[R] Getting rid of for loops
Kevin J Emerson
kemerson at uoregon.edu
Mon Jul 17 04:18:10 CEST 2006
Hello R-users!
I have a style question. I know that for loops are somewhat frowned upon in
R, and I was trying to figure out a nice way to do something without using
loops, but figured that i could get it done quickly using them. I am now
looking to see what kind of tricks I can use to make this code a bit more
aesthetically appealing to other R users (and learn something about R along
the way...).
Here's the problem. I have a data.frame with 4 columns of dependent
variables and then ~35 columns of predictor variables (factors) [for those
interested, it is a qtl problem, where the predictors are genotypes at DNA
markers and the dependent variable is a biological trait]. I want to go
through all pairwise combinations of predictor variables and perform an
anova with two predictors and their interaction on a given dependent
variable. I then want to store the p.value of the interaction term, along
with the predictor variable information. So I want to end up with a
dataframe at the end with the two variable names and the interaction p value
in each row, for all pairwise combinations of predictors. I used the
following code:
# qtl is the original data.frame, and my dependent var in this case is
# qtl$CPP.
marker1 <- NULL
marker2 <- NULL
p.interaction <- NULL
for ( i in 5:40) { # cols 5 - 41 are the predictor factors
for (j in (i+1):41) {
marker1 <- rbind(marker1,names(qtl)[i])
marker2 <- rbind(marker2,names(qtl)[j])
tmp2 <- summary(aov(tmp$CPP ~ tmp[,i] * tmp[,j]))[[1]]
p.interaction <- rbind(p.interaction, tmp2$"Pr(>F)"[3])
}
}
I have two questions:
(1) is there a nicer way to do this without having to invoke for loops?
(2) my other dependent variables are categorical in nature. I need
basically the same information - I am looking for information regarding the
interaction of predictors on a categorical variable. Any ideas on what
tests to use? (I am new to analysis of all-categorical data).
Thanks in advance!
Kevin
--------------------------------------
--------------------------------------
Kevin Emerson
Center for Ecology and Evolutionary Biology
1210 University of Oregon
Eugene, OR 97403
USA
kemerson at uoregon.edu
More information about the R-help
mailing list