[R] reshape dataframe to array (pivot table)
Rainer Schuermann
Rainer.Schuermann at GMX.net
Tue Jan 24 19:28:43 CET 2012
Hi,
I wouldn't know how to fill the data into the array form you want, but
you can get the aggregated data with
dfm <- melt( df )
dfc <- cast( dfm, LOC ~ variable, sum )
> dfc
LOC SPEC1 SPEC2
1 1 23 5
2 2 23 2
3 3 0 0
Hope this helps as a first step!
Rgds,
Rainer
On 2012-01-24 17:15, Johannes Radinger wrote:
> Hello,
>
> I would like to reshape a dataframe into an array. This is kind a similar task as Excel performs with a Pivot table. To illustrate it:
>
> LOC<- factor(c(1,2,2,3,1,1))
> SPEC1<- c(0,0,23,0,12,11)
> SPEC2<- c(1,2,0,0,0,4)
>
> df<- data.frame(LOC,SPEC1,SPEC2) # original dataframe
>
> a<- array(NA,dim=c(length(levels(LOC)),1,2),dimnames=list(levels(LOC),"LOC",c("SPEC1","SPEC2"))) #new array set up, how it should look like
>
> The final array is splitted by the SPEC (for each SPEC an new layer), and
> the LOC is collapsed so that there is only one line per level of LOC.
> The array should get populated with:
> 1) the sums
> 2) and presence/absence (can be easily calculated from sums)
>
> What is the best way to do that? I looked into reshape and apply...probably one of them is suitable but I don't know how...Or has that to be done with a loop?
>
> cheers,
> /johannes
More information about the R-help
mailing list