R - 重塑数据框和聚合元素

R - Reshaping data frame and aggregate elements

我有这样一个数据框

client  Product date
A        apple  May
B        grape  Jun
B        apple  Jun
C        apple  Jul
A        grape  Jul

我想要这个:

 client May Jun Jul
  A     1   0   1
  B     0   2   0
  C     0   0   1

也就是说,我想汇总每个客户几个月内销售的所有产品。我知道我尝试了 reshape 和 data.table 但我想不出最好的方法。

谢谢

as.data.frame.matrix(xtabs(f~client+date,cbind(f=1,dat)))
  Jul Jun May
A   1   0   1
B   0   2   0
C   1   0   0

或者你可以这样做:

as.data.frame.matrix(table(dat[-2]))
  Jul Jun May
A   1   0   1
B   0   2   0
C   1   0   0

这也可以用 Dplyr 完成

library(tidyverse)


client <- factor(c("A","B","B","C","A"))
product <- factor(c("apple", "grape", "apple", "apple", "grape"))
date <- factor(c("May", "Jun", "Jun", "Jul", "Jul"))

df <- data.frame(client=client,
                 product=product,
                 date=date)

df_sum <- df %>%
  group_by(client, date) %>%
  summarise(n=n()) %>%
  spread(date, n, fill = 0)