[R] Reshaping data

From: Rau, Roland <Rau_at_demogr.mpg.de>
Date: Thu 08 Dec 2005 - 19:50:48 EST


Dear all,

given I have data in a data.frame which indicate the number of people in a
specific year at a specific age:

n <- 10
mydf <- data.frame(yr=sample(1:10, size=n, replace=FALSE),

                   age=sample(1:12, size=n, replace=FALSE),
                   no=sample(1:10, size=n, replace=FALSE))

Now I would like to make a matrix with (in this simple example) 10 columns (for the years) and 12 rows (for the ages). In each cell, I would like to put the correct number of individuals.

So far I was doing this as follows:

mymatrix <- matrix(0, ncol=10, nrow=12)
for (year in unique(mydf$yr)) {
  for (age in unique(mydf$age)) {
    if (length(mydf$no[mydf$yr==year & mydf$age==age]) > 0) {       mymatrix[age,year] <- mydf$no[mydf$yr==year & mydf$age==age]     } else {
      mymatrix[age,year] <- 0
    }
  }
}

This is fairly fast in such a simple setting. But with more years and ages (and for roughly 300 datasets) this becomes pretty slow. And in addition, this is not really elegant R-code.

Can somebody point me into the direction how I can do that in a more elegant
way, possibly avoiding the loops?

Thanks,
Roland

+++++
This mail has been sent through the MPI for Demographic Rese...{{dropped}}



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 Thu Dec 08 20:11:11 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:34 EST