Pavlo Razumovskyi
Pavlo Razumovskyi

Reputation: 824

R, DataFrame - group by multiple rows

I have a data.frame about posts looks like this:

 post_id   group_id hour(when posted) likes
 1         1        13                  5
 2         1        13                  6
 3         1        23                  3
 4         2        12                  30
 5         2        13                  34
 6         2        22                  10

I want to plot likes distribution by hours in each group, so I need a data.frame like this one:

          0 ... 12 13 ... 22 23   <- hours
gorup#1         0  11         3   <- sum of likes in group#i in xx hour
group#2         30 34     10  0

How can I group post by group and by hour?

Upvotes: 0

Views: 248

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193497

Assuming your data.frame is called "mydf", perhaps you can try xtabs (since you're just looking for sum):

> xtabs(likes ~ group_id + hour, mydf)
        hour
group_id 12 13 22 23
       1  0 11  0  3
       2 30 34 10  0

To get all the levels for "hour", even if they sum to "0" for all groups, factor the "hour" column first.

Another convenient alternative is to use dcast from the "reshape2" package.

Upvotes: 1

Related Questions