Search code examples
rreshape2dcast

R dcast: How do I perform dcast value calculations?


I have a large data frame, with which I run this:

dcast(mydata, People ~ Categories, value.var = "Answer Count", **sum**)

and this is the result:

People  category1   category2
Marge   3,648   6,402
Homer   3,586   6,684
Bart    3,469   7,119
Lisa    4,045   6,758
Maggie  2,847   5,748

Also, this:

dcast(mydata, People ~ Categories, value.var = "Answer Count", **length**)

Makes this:

People  category1   category2
Marge   2,531   4,516
Homer   2,535   4,512
Bart    2,542   4,563
Lisa    2,501   4,488
Maggie  2,517   4,513

Effectively, I want to do this:

dcast(mydata, People ~ Categories, value.var = "Answer Count", **sum / length / 6**)

and get these values:

People  category1   category2
Marge   0.240221256 0.236271036
Homer   0.235765943 0.246897163
Bart    0.227445581 0.260026298
Lisa    0.269558843 0.250965538
Maggie  0.188518077 0.212275648

I've tried manipulating fun.aggregate as an argument but I'm not sure that's the correct path, or I don't know what I'm doing. Could someone help me with this please? (Sidenote: this sample has two categories. The real data has >40.)


Solution

  • We can use an anonymous function call in fun.aggregate

    library(reshape2)
    dcast(mydata, People ~ Categories, 
       value.var = "Answer Count", fun.aggregate = function(x) sum(x)/length(x))