某些操作在数据框中具有唯一计数和总计数

时间:2019-12-21 11:27:08

标签: r dataframe

所以我有以下数据:

enter image description here

我希望其输出如下:

enter image description here

由于我的最后一个问题,我能够获得一半的输出。

哪个是

enter image description here

但是如何获取剩余列的输出?我尝试了一些溢出但没有运气的循环。

  

链接:

     

SampleData.csv

     

LINK to the previous question

1 个答案:

答案 0 :(得分:1)

您可以尝试:

library(dplyr)

df %>%
  group_by(Company, Date) %>%
  summarise(Items = toString(ID),
            No.of.Items.Bought = n_distinct(ID)) %>%
  mutate(No.of.Items.Did.Not.Buy = max(No.of.Items.Bought) - No.of.Items.Bought)