[R] Reshaping data

Rau, Roland Rau at demogr.mpg.de
Thu Dec 8 10:29:46 CET 2005


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}}




More information about the R-help mailing list