[R] Using string vectors as for loop arguments

From: Kevin Middleton <kmm_at_brown.edu>
Date: Sat 04 Feb 2006 - 08:41:11 EST


Is there a mechanism to interate through a vector of strings? Say I have a data frame of 50 variables (VAR1 to VAR50), each with 100 measurements along with some coding factors (EXP and DOSE). I want to calculate the mean of a subset of each of VAR1 to VAR 50 (selecting by EXP and DOSE). Rather than just copy/pasting the same code 50 times, I thought of using a for loop to go through a vector of VAR1 to VAR50 (VAR.LIST below). Each iteration would be the variable name for which the mean would be calculated.

Trying this, I get an error that the argument is not numeric or logical. So I assume that for loops with vectors of strings are disallowed. What I am wondering is whether there a way to mimic this sort of procedure?

The code looked something like this:

VAR.LIST<-paste(c("VAR"), 1:50, sep="")

for (i in VAR.LIST){

	mean(i[EXP==1 & DOSE==1], na.rm=T)
	}



Thanks
Kevin Middleton



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 Sat Feb 04 08:48:28 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:22 EST