If I have a table like this:
user,v1,v2,v3
a,1,0,0
a,1,0,1
b,1,0,0
b,2,0,3
c,1,1,1
How to I turn it into this?
user,v1,v2,v3
a,2,0,1
b,3,0,3
c,1,1,1
If I have a table like this:
user,v1,v2,v3
a,1,0,0
a,1,0,1
b,1,0,0
b,2,0,3
c,1,1,1
How to I turn it into this?
user,v1,v2,v3
a,2,0,1
b,3,0,3
c,1,1,1
We can use the base R
aggregate
for getting the sum
of columns by group. Here, we use the formula method where .
implies all other variables, while the 'user' on the RHS of ~
is the grouping variable. We specify the FUN=sum
to get the sum
of all other columns.
aggregate(.~user, df1, sum)
Or we can use data.table
. We convert the 'data.frame' to 'data.table' (setDT(df1)
), grouped by 'user', we loop (lapply
) through the subset of data.table (.SD
) and get the sum
.
library(data.table)
setDT(df1)[, lapply(.SD, sum), by=user]
Or we use dplyr
, grouped by 'user', we get the sum
of all other columns within summarise_each
.
library(dplyr)
df1 %>%
group_by(user) %>%
summarise_each(funs(sum))