Re: [R] Reshaping data

From: Rau, Roland <Rau_at_demogr.mpg.de>
Date: Thu 08 Dec 2005 - 20:29:46 EST


Hi,

thank you very much for your fast reply. It worked fine. In the meantime, I also had now an idea using a function from the apply-family (see below for the code).

The more I use R, the more I get the impression that either "the apply-family" or outer() can solve most of my data-transformation questions/problems. Is this a typical learning experience?

Best,
Roland

> -----Original Message-----
> From: Dimitris Rizopoulos
> just try
>
> mymatrix <- matrix(0, 12, 10)
> mymatrix[cbind(mydf$age, mydf$yr)] <- mydf$no
> mymatrix

### generating the data
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))
###
newmatrix <- tapply(X=mydf$no, INDEX=list(year=mydf$age, age=mydf$yr), FUN=sum)
newmatrix[is.na(newmatrix)] <- 0

+++++
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:50:18 2005

This archive was generated by hypermail 2.1.8 : Thu 08 Dec 2005 - 23:37:01 EST