根据特定数据范围汇总基于其他列的值

时间:2020-01-24 04:12:19

标签: r

我有以下数据集:

name<-c("John","John","John","John","John","John","John")
Dealer<-c("ASD","ASD","ASD","ASD","ASD","ASD","ASD")
Date<-c("2020-01-03","2020-01-04","2020-01-05","2020-01-06","2020-01-07","2020-01-08","2020-01-09")
dataset<-data.frame(name,Dealer,Date)

,我想创建一个数据框,该框总结为Dealer,根据我们位于最近的事实来显示name的前一天的数量和name的最近7天的数量日期("2020-01-09")。因此,新数据框将如下所示:

Dealer2 PreviousDay PreviousWeek
     ASD           1            7

1 个答案:

答案 0 :(得分:1)

我们可以基于arrange Date来计算数据,并计算倒数第二天的条目数和每个Dealer的最近7天的条目数。

library(dplyr)

dataset %>%
  mutate(Date = as.Date(Date)) %>%
  arrange_all %>%
  group_by(Dealer) %>%
  summarise(PreviousDay = sum(Date == last(Date) - 1), 
            PreviousWeek = sum(Date %in% (last(Date) - 7) : last(Date)))

# A tibble: 1 x 3
#  Dealer PreviousDay PreviousWeek
#  <fct>        <int>        <int>
#1 ASD              1            7