Using string vectors as for loop arguments

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using string vectors as for loop arguments

Kevin Middleton
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Using string vectors as for loop arguments

Bert Gunter
?apply

as in:

answer<-apply(yourframe,2,function(x)x[EXP==1 & DOSE==1])

Note that there are slicker ways to do this call.
Note also for that for the particular case of column means, you have a
built-in much faster alternative:

answer<-colMeans(yourframe[EXP==1 & DOSE==1,],na.rm=TRUE)

Also note that the == construction may be problematic if EXP or DOSE are not
factors, say.

Finally, you need to start reading the R docs to learn about this yourself.
"An Introduction to R" is a good place to start.

Oh... and your attempt below will certainly not work. After you read the
basic docs, you'll see why not.


-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA


> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Kevin Middleton
> Sent: Friday, February 03, 2006 1:41 PM
> To: [hidden email]
> Subject: [R] Using string vectors as for loop arguments
>
> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html