[R] aggregate and stack

Stephen D. Weigand weigand.stephen at charter.net
Thu May 26 04:44:56 CEST 2005


Dear Paulo,

On May 25, 2005, at 8:01 PM, Paulo Brando wrote:

> Dear All,
>
> I have tried to calculate tree mean growth but I think the structure I  
> used below (growthresumo) is not the most elegant, even though it  
> worked. The only problem I had in this first part was that  I cannot  
> use 'summary', just 'mean' (sorry but 'R' is pretty new for me).
>

In case you didn't notice, help(aggregate) indicates that 'FUN'
should be a scalar function, so summary won't work for that reason.

>> growthresumo <-  
>> aggregate(growth[,c(16,19,23,27,31,35,39,43,47,52,56,60,64,68,72,76,81 
>> ,85,89,93,97,101,105,109,113,117,121,125,129,133,137,
> 141,145,149,153,157,161,165,169,173,177,181,185,189,194,197,201,205,209 
> ,213,217,221,225,229,233,237,241)],
> by=(growth[,c(3,8)]),MEAN,na.rm=TRUE)
>

It's hard to know where 'growth' came from. Is it your own data.frame,
or from a package? It's better to provide a reproducible or toy example
(as you'll often read here).

> #after growth is calculated, I want to stack the results in just one  
> colunm.
>
>> growthvertical <- c(growthresumo[,3],...,growthresumo[,50]) # this is  
>> very time consuming though
>

This comes to my mind:

as.vector(as.matrix(growthresumo[,3:50]))

but look up the help on stack() because it's a very powerful tool that
is aptly named (and might do everything you want).

>> Parcel <- c("C9","S8"..."C9","S8") # 50 items
>

rep() could help with the above.

>> date < c("DATE1"...."DATE50")
>

paste() will help with this.

>> growthpermonth <- data.frame(Parcel, Date, growthvertical)
>
> Thank you very much!

Good luck with R!

Stephen




More information about the R-help mailing list