R 按多列汇总数据帧

R 按多列汇总数据帧,r,dataframe,R,Dataframe,我想知道如何在R中总结更多的专栏。 我有这样一个data.frame(df): device customer message count_today count_yesterday 1 x a 5 3 1 x b 1 2 2 y c 0 1 3 z

我想知道如何在R中总结更多的专栏。 我有这样一个data.frame(df):

device  customer  message  count_today  count_yesterday
1       x          a        5              3
1       x          b        1              2
2       y          c        0              1
3       z          a        5              2
1       x          a        7              4
我想获得每个客户的邮件计数摘要。 这是我希望得到的结果:

customer  count_today  count_yesterday
x          13            9
y          0             1
z          5             2
我尝试了summary函数来获得count_today和count_today的部分结果,但是它似乎在总结每个设备的出现次数

这是我尝试的代码:

df_today <- select(df, customer, count_today) %>%
  group_by(cutomer) %>%
    summarise(count_today=n()) %>%
      arrange(., customer)

df_yesterday <- select(df, customer, count_yesterday) %>%
  group_by(cutomer) %>%
    summarise(count_yesterday=n()) %>%
      arrange(., customer)

df_final <-merge(x = df_today, y = df_yesterday, by = "customer", all.x = TRUE, all.y = TRUE)

你能帮我做这个吗?
谢谢你的建议。

sum
而不是
n
进行总结,你会得到你想要的结果

library(dplyr)

df %>%
  group_by(customer) %>%
  summarise_at(c("count_today", "count_yesterday"), sum)

# A tibble: 3 x 3
  customer count_today count_yesterday
  <chr>          <int>           <int>
1 x                 13               9
2 y                  0               1
3 z                  5               2
库(dplyr)
df%>%
分组依据(客户)%>%
总结于(c(“今天盘点”、“昨天盘点”)、和
#一个tibble:3x3
今天的顾客数昨天的顾客数
1 x 13 9
2 y 0 1
3 z 5 2

df%>%group\u by(device)%%>%summary\u at(vars(以('count'))开头)、sum)
您可以通过
dput添加数据构建吗?
library(dplyr)

df %>%
  group_by(customer) %>%
  summarise_at(c("count_today", "count_yesterday"), sum)

# A tibble: 3 x 3
  customer count_today count_yesterday
  <chr>          <int>           <int>
1 x                 13               9
2 y                  0               1
3 z                  5               2