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

时间:2018-07-30 18:35:24

标签: r aggregate reshape

我有一个这样的数据框

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

也就是说,我希望汇总每个客户几个月内售出的所有产品。我知道我会尝试重塑data.table,但我不知道执行此操作的最佳方法。

谢谢

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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)