将一个包含周日期的数组聚合成每周的数据。

huangapple go评论59阅读模式
英文:

Aggregating daily to weekly given an array of week dates

问题

以下是翻译好的部分:

在我开始之前,让我说一下,我知道存在各种各样的星期定义,但我很好奇如何在不知道我的数据集中使用的星期定义的情况下解决这个问题。

我想将每日数值求和为每周数值。我有一个代表周末的日期向量(称为“week”),以及一个包含每日频率日期的数据表(称为“dt”)。我需要在“week”中查找每个日期,并将其对应的值以及前面的6个日期相加,以得出一个新的聚合值。转换后的日期应该对应于“week”中的相同日期。

时间序列没有间隙。

week

["2017-01-01", "2017-01-08", ...]

dt

"date","value"
"2016-12-26",1
"2016-12-27",1
"2016-12-28",1
"2016-12-29",1
"2016-12-30",1
"2016-12-31",1
"2017-01-01",1
"2017-01-02",2
"2017-01-03",2
"2017-01-04",2
"2017-01-05",2
"2017-01-06",2
"2017-01-07",2
"2017-01-08",2
...

expected result

"2017-01-01",7
"2017-01-01",14
...
英文:

Before I start, let me say I am aware various definitions of week exist, but I am curious as to how to solve this problem without knowing the definition of the week used in my dataset.

I want to sum daily values into weekly values. I have a vector of dates which represent the end of the week (saying week), and a data.table with daily frequency dates (saying dt). I need to search up each date in week and sum its corresponding value and the 6 before to come up with a new aggregated value. The transformed date should correspond to the same date as in week.

The time series has no gap.

week

["2017-01-01", "2017-01-08", ...]

dt

"date","value"
"2016-12-26",1
"2016-12-27",1
"2016-12-28",1
"2016-12-29",1
"2016-12-30",1
"2016-12-31",1
"2017-01-01",1
"2017-01-02",2
"2017-01-03",2
"2017-01-04",2
"2017-01-05",2
"2017-01-06",2
"2017-01-07",2
"2017-01-08",2
...

expected result

"2017-01-01",7
"2017-01-01",14
...

答案1

得分: 1

你可以循环遍历week_starts,并计算前6天的总和,即:

week_starts <- as.Date(c("2017-01-01", "2017-01-08"))

df$date <- as.Date(df$date)
sums <- sapply(week_starts, \(i) sum(df$value[df$date >= (i - 6) & df$date <= i]))

cbind.data.frame(week_starts, sums)

  week_starts sums
1  2017-01-01    7
2  2017-01-08   14

数据

structure(list(date = structure(c(17161, 17162, 17163, 17164, 
17165, 17166, 17167, 17168, 17169, 17170, 17171, 17172, 17173, 
17174), class = "Date"), value = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L)), row.names = c(NA, -14L), class = "data.frame")
英文:

You can loop over the week_starts and do the sum of previous 6 days, i.e.

week_starts &lt;- as.Date(c(&quot;2017-01-01&quot;, &quot;2017-01-08&quot;))

df$date &lt;- as.Date(df$date)
sums &lt;- sapply(week_starts, \(i) sum(df$value[df$date &gt;= (i - 6) &amp; df$date &lt;= i]))

cbind.data.frame(week_starts, sums)

  week_starts sums
1  2017-01-01    7
2  2017-01-08   14

DATA

structure(list(date = structure(c(17161, 17162, 17163, 17164, 
17165, 17166, 17167, 17168, 17169, 17170, 17171, 17172, 17173, 
17174), class = &quot;Date&quot;), value = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L)), row.names = c(NA, -14L), class = &quot;data.frame&quot;)

huangapple
  • 本文由 发表于 2023年5月11日 19:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定