[R] list of data.frames

Andrew Winterman
2011-07-18T20:11:14Z

Hey there,
I've got a for loop in my code which I'm having difficulty seeing how to
avoid.

Here's the code, with some sample data. You can safely copy and paste into
the interpreter if you want.

#I have some data frames.

a <- as.data.frame(matrix(runif(50), nrow = 10, ncol = 5))
b <- as.data.frame(matrix(runif(50), nrow = 10, ncol = 6))
c <-as.data.frame( matrix(runif(50), nrow = 10, ncol = 4))

#They've been presented in a list, and ideally this code would run for any
reasonable number of data frames.


d <- list(a,b,c)

#let's suppose these data frames are time series data sets, which cover
overlapping years.

#I'm interested in the first column of the first data frame,
#the second column of the second
#and the third column of the third.
#These columns correspond to the same year, lets say 2000,
#and I've written a function which spits out which columns corresponding
#to the year 2000 in a list


L <- list(1,2,3)

#I'm interested in computing:
#   a$x2000/(a$x2000 + b$x2000 + c$x2000)
#in the general case

#Here's the crux of the issue. I'm using the following to compute
proportions of a, b, and c:


T <- matrix(ncol = length(d), nrow = nrow (d[[1]])
         #a matrix to hold the (in this case) three values.

for(i in 1:length(d)) {
   Li <- L[[i]]
#for readability

   T[,i] <-  d[[i]][, Li]
}

#then I sum across the rows to obtain a total
total <- apply(T, 1, sum)

#and then compute proportions for each data set:

prop <- rep(0, length(total))
for(i in 1:length(d)){
 Li <- L[[i]]
prop[i] <- d[[i]][, Li] / total[i]
}



Does anybody have any advice for slimming this down? I feel like this
shouldn't really be so complicated.

	[[alternative HTML version deleted]]