reshape - How to create a pivot table in R with more than 3 variables -
i having problems in create pivot table data frame this:
c1 c2 c3 c4 e 5.76 201 la vista e 47530.71 201 la vista e 82.85 201 la vista l 11376.55 201 la vista e 6683.37 203 la vista e 66726.52 203 la vista e 2.39 203 la vista e 79066.07 202 montoxv_a60d e 14715.71 202 montoxv_a60d e 22661.78 202 montoxv_a60d l 81146.25 124 montoxv_a90d l 471730.2 124 montoxv_a186d e 667812.84 124 montoxv_a186d my problem don't know how create in r pivot table or summary table 4 variables, considering final table in rows, levels of c1 , c3 , columns levels of c4. values of c2 variable must aggregated sum each level considered in rows. this:
la vista montoxv_a60d montoxv_a186d montoxv_a90d e 201 47619.32 0 0 0 e 203 73412.28 0 0 0 e 202 0 116443.56 0 0 e 124 0 0 667812.84 0 l 201 11376.55 0 0 0 l 124 0 0 471730.2 81146.25
you can dcast reshape2 package:
dcast(mydata, c1 + c3 ~ c4, value.var="c2", fun.aggregate=sum) for example:
library(reshape2) # reproducible version of data mydata = read.csv(text="c1,c2,c3,c4 e,5.76,201,a la vista e,47530.71,201,a la vista e,82.85,201,a la vista l,11376.55,201,a la vista e,6683.37,203,a la vista e,66726.52,203,a la vista e,2.39,203,a la vista e,79066.07,202,montoxv_a60d e,14715.71,202,montoxv_a60d e,22661.78,202,montoxv_a60d l,81146.25,124,montoxv_a90d l,471730.2,124,montoxv_a186d e,667812.84,124,montoxv_a186d", header=true) result = dcast(mydata, c1 + c3 ~ c4, value.var="c2", fun.aggregate=sum) produces:
c1 c3 la vista montoxv_a186d montoxv_a60d montoxv_a90d 1 e 124 0.00 667812.8 0.0 0.00 2 e 201 47619.32 0.0 0.0 0.00 3 e 202 0.00 0.0 116443.6 0.00 4 e 203 73412.28 0.0 0.0 0.00 5 l 124 0.00 471730.2 0.0 81146.25 6 l 201 11376.55 0.0 0.0 0.00
Comments
Post a Comment