数据框架中不同日期列的条件列的总和。

问题描述 投票:0回答:1

嗨,我有一个数据框架为 。

order_number   created_at     invoiced_at   shipped_at  quantity
UT637RR        2020-01-04     2020-01-06    2020-01-08  45
JYWEDER        2020-03-04     2020-03-04    2020-03-11  15
KFUV89R        2020-02-07     2020-02-13    2020-02-18  23
USKUV8R        2020-01-14     2020-01-16    2020-01-18  22
WUYT8RR        2020-02-13     2020-01-23    2020-01-30  12

我想总结一下昨天创建了多少数量,开了多少数量的发票,发货了多少数量。

我试过这个方法,但我没有得到想要的结果。

df <- df %>% 
  select(processed_quantity,i_d,s_d,c_d) %>% 
  group_by(i_d,s_d,c_d) %>%
  summarise(id = sum(processed_quantity),sd = sum(processed_quantity),cd = sum(processed_quantity))
r dataframe dplyr
1个回答
1
投票

下面是解决方案。

library(dplyr)

df %>%
  gather(type,date,-order_number,-quantity) %>%
  group_by(type,date) %>%
  summarise(quantity = sum(quantity) %>%
  filter(date == //yesterday) # here you should put the actual date you are looking for

这个方案首先创建一个数据框,其中一列是日期类型(创建,发货,开票),一列是实际日期。

然后我们使用 group_by() 以汇总每个日期和类型的数量。

© www.soinside.com 2019 - 2024. All rights reserved.