[R] iterating over groups of columns

09wkj Bill.K.Jannen at williams.edu
Tue Jun 8 22:15:06 CEST 2010


I am mainly a Java/C++ programmer, so my mind is used to iterating over data with for loops. After a long break, I am trying to get back into the "R mindset", but I could not find a solution in the documentation for the applys, aggregate, or by.

I have a data.frame where each row is an entry with 10 groups of measurements. The first measurement spans 1 column, the second spans 2 columns, third 3, and so on (55 total columns). What I want to do is add to my data.frame 10 new columns containing the minimum value of each measurement.

dim(the.data)
[1] 1679  55

> colnames(the.data)
  [1] "k.1.1"   "k.2.1"   "k.2.2"   "k.3.1"   "k.3.2"   "k.3.3"   "k.4.1"  
  [8] "k.4.2"   "k.4.3"   "k.4.4"   "k.5.1"   "k.5.2"   "k.5.3"   "k.5.4"  
 [15] "k.5.5"   "k.6.1"   "k.6.2"   "k.6.3"   "k.6.4"   "k.6.5"   "k.6.6"  
 [22] "k.7.1"   "k.7.2"   "k.7.3"   "k.7.4"   "k.7.5"   "k.7.6"   "k.7.7"  
 [29] "k.8.1"   "k.8.2"   "k.8.3"   "k.8.4"   "k.8.5"   "k.8.6"   "k.8.7"  
 [36] "k.8.8"   "k.9.1"   "k.9.2"   "k.9.3"   "k.9.4"   "k.9.5"   "k.9.6"  
 [43] "k.9.7"   "k.9.8"   "k.9.9"   "k.10.1"  "k.10.2"  "k.10.3"  "k.10.4" 
 [50] "k.10.5"  "k.10.6"  "k.10.7"  "k.10.8"  "k.10.9"  "k.10.10"

I want to add to the.data new columns: min.k.1, min.k.2, ..., min.k.10

This is the section of code I would like to improve, hopefully getting rid of the eval and the for loop:

for(k in 1:10){
    s <- subset(the.data, select=paste("k", k, 1:k, sep="."))
    eval(parse(text = paste("the.data$min.k.", k, "<-as.vector(by(s, 1:nrow(s), min))", sep="")))
}

Thanks for any help,
Bill


More information about the R-help mailing list