[R] Getting rid of for loops

From: Kevin J Emerson <kemerson_at_uoregon.edu>
Date: Mon 17 Jul 2006 - 12:18:10 EST


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@uoregon.edu

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Jul 17 12:24:58 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 18 Jul 2006 - 02:19:27 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.