首页 > 解决方案 > 将每日数据转换为每周数据并汇总 R 中的多个列

问题描述

我想更改以下数据集:

date          A   B
01/01/2018  391 585
02/01/2018  420 595
03/01/2018  455 642
04/01/2018  469 654
05/01/2018  611 900
06/01/2018  449 640
07/01/2018  335 522
08/01/2018  726 955
09/01/2018  676 938
10/01/2018  508 740
11/01/2018  562 778
12/01/2018  561 761
13/01/2018  426 609
14/01/2018  334 508

我想要的输出如下:

date           A       B
07/01/2018  3130    4538
14/01/2018  3793    5289

其中,A 列和 B 列的数量是每周 7 天的总和。确实,我想将每日数据转换为每周数据。我在 Stackoverflow 网站上找到了两个解决方案。一种解决方案是使用库(tidyquant)和以下代码

library(tidyquant)
newfd<-df %>%
  tq_transmute(select     = A,
               mutate_fun = apply.weekly,
               FUN        = sum)

该代码为 A 列生成每周数据,而我需要所有列。(我有很多专栏)。我还使用了以下代码。但是,我不知道如何为所有列开发代码。

library(slider)   
slide_period_dfr(.x = califo, .i=as.Date(califo$date), 
                 .period = "week", 
                 .f = ~data.frame(week_ending = tail(.x$ date,1),
                                  week_freq = sum(.x$A)),
                 .origin = as.Date("2018-01-01"))

标签: raggregationsummarizetidyquant

解决方案


您可以使用inceiling_date将日期设为每周日期和sum多个变量。acrossdplyr

library(dplyr)
library(lubridate)

df %>%
  group_by(date = ceiling_date(dmy(date), 'week', week_start = 1)) %>%
  summarise(across(A:B, sum))

#  date           A     B
#  <date>     <int> <int>
#1 2018-01-08  3130  4538
#2 2018-01-15  3793  5289

数据

df <- structure(list(date = c("01/01/2018", "02/01/2018", "03/01/2018", 
"04/01/2018", "05/01/2018", "06/01/2018", "07/01/2018", "08/01/2018", 
"09/01/2018", "10/01/2018", "11/01/2018", "12/01/2018", "13/01/2018", 
"14/01/2018"), A = c(391L, 420L, 455L, 469L, 611L, 449L, 335L, 
726L, 676L, 508L, 562L, 561L, 426L, 334L), B = c(585L, 595L, 
642L, 654L, 900L, 640L, 522L, 955L, 938L, 740L, 778L, 761L, 609L, 
508L)), class = "data.frame", row.names = c(NA, -14L))

推荐阅读