gpt4 book ai didi

r - 根据条件创建累积每周平均值的 Date.table 解决方案

转载 作者:行者123 更新时间:2023-12-04 02:31:25 24 4
gpt4 key购买 nike

我每天测量 x 数据。每个唯一 ID (a, b) 都有多行数据。对于每个唯一 ID,我想计算第 1-2 周的每周平均值和第 3 周的累计每周平均值 - (结束日期)属于 Start.date 和 End.date 的 x 数据。每个每周平均值将是一个新列。

以下代码通过为每个唯一 ID 创建一个周变量来计算每周平均值:

dcast(dat[,
week := floor(difftime(Date, Start.date, units = "weeks")) + 1,
by = .(ID)][,
.(weekly_mean = mean(x)),
by = .(ID, week)],
ID ~ paste("week", week, sep = "_"),
value.var = "weekly_mean")

我可以根据现有的每周平均值创建这些累积的每周平均值吗?如果是这样,一旦我有了累积平均值列,我最终是否可以删除第 3 周(结束日期)的每周平均值?

我希望生成的数据集看起来如何:

Date x Start.Date End.Date ID. Week_1 Week_2 Week_3_cum Week_4_cum ...

一些数据:


dat <- structure(list(Date = structure(c(1104969600, 1105056000,
1105142400, 1105228800, 1105315200, 1105401600, 1105488000, 1105574400,
1105660800, 1105747200, 1105833600, 1105920000, 1106006400, 1106092800,
1106179200, 1106265600, 1106352000, 1106438400, 1106524800, 1106611200,
1106697600, 1106784000, 1106870400, 1106956800, 1107043200, 1107129600,
1107216000, 1107302400, 1107388800, 1107475200, 1107561600, 1107648000,
1107734400, 1107820800, 1107907200, 1107993600, 1108080000, 1108166400,
1108252800, 1108339200, 1108425600, 1108512000, 1108598400, 1108684800,
1108771200, 1108857600, 1108944000, 1109030400, 1109116800, 1109203200,
1109289600, 1109376000, 1109462400, 1109548800, 1104969600, 1105056000,
1105142400, 1105228800, 1105315200, 1105401600, 1105488000, 1105574400,
1105660800, 1105747200, 1105833600, 1105920000, 1106006400, 1106092800,
1106179200, 1106265600, 1106352000, 1106438400, 1106524800, 1106611200,
1106697600, 1106784000, 1106870400, 1106956800, 1107043200, 1107129600,
1107216000, 1107302400, 1107388800, 1107475200, 1107561600, 1107648000,
1107734400, 1107820800, 1107907200, 1107993600, 1108080000, 1108166400,
1108252800, 1108339200, 1108425600, 1108512000, 1108598400, 1108684800,
1108771200, 1108857600, 1108944000, 1109030400, 1109116800, 1109203200,
1109289600, 1109376000, 1109462400, 1109548800), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), x = c(9.5, 9.4, 9.7, 10.11, 11.11,
12.11, 13.11, 14.11, 15.11, 10.11, 11.11, 12.11, 13.11, 14.11,
15.11, 9.5, 9.4, 9.7, 10.11, 11.11, 12.11, 13.11, 14.11, 15.11,
10.11, 11.11, 12.11, 13.11, 14.11, 15.11, 9.5, 9.4, 9.7, 10.11,
11.11, 12.11, 13.11, 14.11, 15.11, 10.11, 11.11, 12.11, 13.11,
14.11, 15.11, 9.5, 9.4, 9.7, 10.11, 11.11, 12.11, 13.11, 14.11,
15.11, 10.11, 11.11, 12.11, 13.11, 14.11, 15.11, 9.5, 9.4, 9.7,
10.11, 11.11, 12.11, 13.11, 14.11, 15.11, 10.11, 11.11, 12.11,
13.11, 14.11, 15.11, 9.5, 9.4, 9.7, 10.11, 11.11, 12.11, 13.11,
14.11, 15.11, 10.11, 11.11, 12.11, 13.11, 14.11, 15.11, 9.5,
9.4, 9.7, 10.11, 11.11, 12.11, 13.11, 14.11, 15.11, 16.11, 17.11,
18.11, 19.11, 20.11, 21.11, 22.11, 23.11, 24.11), Start.Date = structure(c(1104969600,
1104969600, 1104969600, 1104969600, 1104969600, 1104969600, 1104969600,
1104969600, 1104969600, 1104969600, 1104969600, 1104969600, 1104969600,
1104969600, 1104969600, 1104969600, 1104969600, 1104969600, 1104969600,
1104969600, 1104969600, 1104969600, 1104969600, 1104969600, 1104969600,
1104969600, 1104969600, 1104969600, 1104969600, 1104969600, 1104969600,
1104969600, 1104969600, 1104969600, 1104969600, 1104969600, 1104969600,
1104969600, 1104969600, 1104969600, 1104969600, 1104969600, 1104969600,
1104969600, 1104969600, 1104969600, 1104969600, 1104969600, 1104969600,
1104969600, 1104969600, 1104969600, 1104969600, 1104969600, 1104883200,
1104883200, 1104883200, 1104883200, 1104883200, 1104883200, 1104883200,
1104883200, 1104883200, 1104883200, 1104883200, 1104883200, 1104883200,
1104883200, 1104883200, 1104883200, 1104883200, 1104883200, 1104883200,
1104883200, 1104883200, 1104883200, 1104883200, 1104883200, 1104883200,
1104883200, 1104883200, 1104883200, 1104883200, 1104883200, 1104883200,
1104883200, 1104883200, 1104883200, 1104883200, 1104883200, 1104883200,
1104883200, 1104883200, 1104883200, 1104883200, 1104883200, 1104883200,
1104883200, 1104883200, 1104883200, 1104883200, 1104883200, 1104883200,
1104883200, 1104883200, 1104883200, 1104883200, 1104883200), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), End.Date = structure(c(1109635200,
1109635200, 1109635200, 1109635200, 1109635200, 1109635200, 1109635200,
1109635200, 1109635200, 1109635200, 1109635200, 1109635200, 1109635200,
1109635200, 1109635200, 1109635200, 1109635200, 1109635200, 1109635200,
1109635200, 1109635200, 1109635200, 1109635200, 1109635200, 1109635200,
1109635200, 1109635200, 1109635200, 1109635200, 1109635200, 1109635200,
1109635200, 1109635200, 1109635200, 1109635200, 1109635200, 1109635200,
1109635200, 1109635200, 1109635200, 1109635200, 1109635200, 1109635200,
1109635200, 1109635200, 1109635200, 1109635200, 1109635200, 1109635200,
1109635200, 1109635200, 1109635200, 1109635200, 1109635200, 1109548800,
1109548800, 1109548800, 1109548800, 1109548800, 1109548800, 1109548800,
1109548800, 1109548800, 1109548800, 1109548800, 1109548800, 1109548800,
1109548800, 1109548800, 1109548800, 1109548800, 1109548800, 1109548800,
1109548800, 1109548800, 1109548800, 1109548800, 1109548800, 1109548800,
1109548800, 1109548800, 1109548800, 1109548800, 1109548800, 1109548800,
1109548800, 1109548800, 1109548800, 1109548800, 1109548800, 1109548800,
1109548800, 1109548800, 1109548800, 1109548800, 1109548800, 1109548800,
1109548800, 1109548800, 1109548800, 1109548800, 1109548800, 1109548800,
1109548800, 1109548800, 1109548800, 1109548800, 1109548800), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), ID = c("a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b")), row.names = c(NA,
-108L), class = c("tbl_df", "tbl", "data.frame"))

setDT(dat)

最佳答案

我相信这是一种方法。

请注意,虽然您提到了累积均值,但 cummean() 在 R 中意味着某些东西。cummean() 返回与输入长度相同的向量,因此不会聚合成一个值。所以步骤是:

  1. 聚合以找到每个组的计数和sum(O3)
  2. 在前两周,简单地求出每周的平均值。
  3. 对于剩余的几周,使用 cumsum 计算运行平均值。
  4. dcast 到您的预期结果中。
library(data.table)

dat[,week:= floor(difftime(AQ.Date, Start.Date, uni = "weeks")) + 1, by = ID]

ans = dat[,
.(.N, weekly_sum = sum(O3)),
by = .(ID, week)
][,
{
lgl_ind = week < 3
first_weeks = which(lgl_ind)
last_weeks = which(!lgl_ind)

out = numeric(length(week))
out[first_weeks] = weekly_sum[first_weeks] / N[first_weeks]
out[last_weeks] = cumsum(weekly_sum[last_weeks]) / cumsum(N[last_weeks])

calc_type = c(rep('mean', length(first_weeks)),
rep('cummean', length(last_weeks)))

list(week, weekly_mean = out, calc_type)
},
by = ID]

ans
#> ID week weekly_mean calc_type
#> <char> <difftime> <num> <char>
#> 1: a 1 weeks 10.72000 mean
#> 2: a 2 weeks 12.82429 mean
#> 3: a 3 weeks 11.00571 cummean
#> 4: a 4 weeks 11.84357 cummean
#> 5: a 5 weeks 11.65952 cummean
#> 6: a 6 weeks 11.87929 cummean
#> 7: a 7 weeks 11.81886 cummean
#> 8: a 8 weeks 11.98025 cummean
#> 9: b 1 weeks 12.61000 mean
#> 10: b 2 weeks 10.72000 mean
#> 11: b 3 weeks 12.82429 cummean
#> 12: b 4 weeks 11.91500 cummean
#> 13: b 5 weeks 12.17048 cummean
#> 14: b 6 weeks 11.95071 cummean
#> 15: b 7 weeks 12.58257 cummean
#> 16: b 8 weeks 13.90366 cummean

dcast(ans,
ID ~ paste("week", week,calc_type, sep = "_"),
value.var = 'weekly_mean')
#> ID week_1_mean week_2_mean week_3_cummean week_4_cummean week_5_cummean
#> <char> <num> <num> <num> <num> <num>
#> 1: a 10.72 12.82429 11.00571 11.84357 11.65952
#> 2: b 12.61 10.72000 12.82429 11.91500 12.17048
#> week_6_cummean week_7_cummean week_8_cummean
#> <num> <num> <num>
#> 1: 11.87929 11.81886 11.98025
#> 2: 11.95071 12.58257 13.90366

关于r - 根据条件创建累积每周平均值的 Date.table 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63975552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com