[R] neater way to create data frame?

Petr Pikal petr.pikal at precheza.cz
Tue Nov 9 10:25:42 CET 2004


Hi Louize

On 8 Nov 2004 at 22:58, Louize Hill wrote:

> Hello,
> 
> I have a large data frame and am aiming to create a summary data frame
> in order to plot quarterly means by age. However, it seems as though I
> am taking a very long winded approach to this - can anybody point me
> in the direction of something neater?
> 
> #original table is dat2,
> dat5<- tapply (dat2$col1, INDEX=list(dat2$Age, dat2$Quarter),
> FUN=mean)

Maybe better approach would be to use

aggregate

dat5<- aggregate (dat2$col1, list(Age=dat2$Age, 
Quarter=dat2$Quarter)

With it you will get  data frame with 3 columns (first two will be 
factors).

and reshape the result, if necessary.


Cheers

Petr

> 
> dat5 <- data.frame(dat5)
> names(dat5) <- c("qt1", "qt2", "qt3", "qt4")
> dat6 <- data.frame(age=row.names(dat5), dat5)
> #transform rownames / 1st column from factor to variables
> dat7 <- as.numeric (as.character(dat6$age))
> #create new table with 5 columns
> dat8 <- cbind (dat7, dat6$qt1, dat6$qt2, dat6$qt3, dat6$qt4)
> #still need to add column names to this new table...
> 
> I am using R2.0.0 on W2K.
> Thanks
> Louize
> 
> ______________________________________________
> R-help at 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

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list