简体   繁体   中英

groupby multiple columns and sum then sum in dplyr

I am grouping dataframe by 3 columns ( v1, v2, v3 ) with dplyr and the sum the 4th column ( v4 ) in this grouping. However, my following code only gives the sum of v4 (a single value) in all dataframe rather using the group.

 df %>%
    group_by(v1, v2, v3) %>%
    summarise(sumv4 = sum(v4))

df

 v1  v2  v3  v4
  1   0   5   1
  1   0   5   1
  1   0   5   0
  2   1   5   1
  2   1   5   0
  3   2   4   1
  3   2   4   0

expected output

  v1  v2  v3  v4
  1   0   5   2
  2   1   5   1
  3   2   4   1

Thanks!

The code I posted works, the issue turns out to be in my package version. It will be solved if specify the package name.

df %>%
group_by(v1, v2, v3) %>%
dplyr::summarise(sumv4 = sum(v4))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM