How to group by two columns in R

前端 未结 2 1923
孤街浪徒
孤街浪徒 2020-12-03 05:44

I have a data frame that I am trying to group and then sum based on two columns. The two columns are characters with one being month and the other variable.

The foll

2条回答
  •  醉酒成梦
    2020-12-03 06:15

    You apparently are not interested in taking your Character [month] as a Date variable. Considering that I'm not wrong you could simply do something like this:

    library(dplyr)
    
    tab %>%
      group_by(month, variable) %>%
      summarise(a_sum=sum(amount),
                a_mean=(mean(amount)))
    

    and get this:

    Source: local data frame [3 x 4]
    Groups: month
    
      month variable a_sum a_mean
    1 1-Jan        x  4000   2000
    2 2-Feb        y  3000   3000
    3 2-Feb        z  5000   5000
    

提交回复
热议问题