【问题标题】:Extract hourly values ( and its indexes) that exceeds monthly averages in R提取超过 R 中每月平均值的每小时值(及其索引)
【发布时间】:2020-09-03 09:17:14
【问题描述】:

我有两个数据框。 df 和 df1。两个数据文件都很大(涵盖了从 01-01-200 到 31-10-2019 的数据),所以我只上传了一个小样本。

df 包含 3 个变量的每小时值及其对应的日期向量,如下所示:

           date           SH1          SH2         SH3        
     2000-01-01 00:00:00 1.941013e-01 1.506780e-01 0.124487891 
     2000-01-01 03:00:00 2.897915e-01 2.743722e-01 0.188432490 
     2000-01-01 06:00:00 3.139408e-01 2.250532e-01 0.001473900 
     2000-01-01 09:00:00 1.845777e-01 1.041934e-01 0.047391565 
     2000-01-01 12:00:00 1.022660e-01 6.179044e-02 0.008843402 
     

df <- structure(list(datex = structure(c(946681200, 946692000, 946702800, 
946713600, 946724400), class = c("POSIXct", "POSIXt")), SH1 = c(0.194101337780203, 
0.289791483274648, 0.313940773547535, 0.184577674010614, 0.102266008573448
), SH2 = c(0.150677966068861, 0.274372218123884, 0.225053245031368, 
0.104193416717294, 0.0617904375526934), SH3 = c(0.12448789070249, 
0.188432490298304, 0.00147390034529415, 0.0473915649486711, 0.00884340207176182
)), class = c("data.table", "data.frame"), row.names = c(NA, 
-5L))

而 df1 是相同数据的月平均值(每年)。看起来像这样:

      date       SH1       SH2       SH3       
  2000-01-01 0.7733497 0.6237698 0.4182768 
  2000-02-01 0.7308772 0.5575175 0.3636893  
  2000-03-01 0.3278784 0.3040463 0.2233942  
  2000-04-01 0.4496596 0.3124064 0.1805953  
  2000-05-01 0.4500503 0.4032727 0.2562054  

df1 <- structure(list(datex = structure(c(10957, 10988, 11017, 11048, 
11078), class = "Date"), SH1 = c(0.773349659462019, 0.730877175434939, 
0.327878366545974, 0.44965959591958, 0.450050258753037), SH2 = c(0.623769804010216, 
0.557517466419755, 0.304046348866025, 0.312406405495768, 0.403272666559865
), SH3 = c(0.418276825782115, 0.36368930844493, 0.223394192812674, 
0.18059530865458, 0.256205390604878)), row.names = c(NA, -5L), class = c("data.table", 
"data.frame"))

我想提取 df 中超过其相应月平均值 (df1) 的值,并获取这些值的位置(索引)。我怎样才能做到这一点?我不是 R 方面的专家,所以请耐心等待。

我认为必须根据两个数据集中的年份和月份进行比较,但我不知道该怎么做。

【问题讨论】:

  • 如果您使这些示例可重现会有所帮助:您可以使用dput()...
  • 我已经编辑了问题并添加了来自dput()的结果
  • data.table 的一件事是您需要删除部分.internal.selfref = ...。在每次输入之后。
  • 另外:你希望结果的格式是什么?
  • 已编辑,我已删除 internal.selfref-.. 部分。我不介意结果是数据框还是数据表,在这种情况下两者都可以工作。

标签: r dataframe data.table


【解决方案1】:

我会这样做:

df %>% 
 group_by(year = year(date), month = month(date)) %>% 
  mutate(
    monthly_average_SH1 = mean(SH1),
    monthly_average_SH2 = mean(SH2),
    monthly_average_SH3 = mean(SH3),
    flag_exceed_SH1 = ifelse(SH1 > monthly_average_SH1, TRUE, FALSE),
    flag_exceed_SH2 = ifelse(SH2 > monthly_average_SH2, TRUE, FALSE),
    flag_exceed_SH3 = ifelse(SH3 > monthly_average_SH3, TRUE, FALSE),
    flag_any_exceed = ifelse(flag_exceed_SH1 | flag_exceed_SH2 | flag_exceed_SH3, TRUE, FALSE)
  ) %>% 
  filter(flag_any_exceed)

这将为您提供一个包含所有超出该值的行的 df。请注意,df1 不是必需的,因为您可以在同一个 df 上生成均值。

如果你想要索引:

df_2 <- df %>% 
  group_by(year = year(date), month = month(date)) %>% 
  mutate(
    monthly_average_SH1 = mean(SH1),
    monthly_average_SH2 = mean(SH2),
    monthly_average_SH3 = mean(SH3),
    flag_exceed_SH1 = ifelse(SH1 > monthly_average_SH1, TRUE, FALSE),
    flag_exceed_SH2 = ifelse(SH2 > monthly_average_SH2, TRUE, FALSE),
    flag_exceed_SH3 = ifelse(SH3 > monthly_average_SH3, TRUE, FALSE),
    flag_any_exceed = ifelse(flag_exceed_SH1 | flag_exceed_SH2 | flag_exceed_SH3, TRUE, FALSE)
  ) 
which(df2$flag_any_exceed)

希望对你有用

【讨论】:

  • 如果我有更多列而不是 3 列,如何以更有效的方式执行此操作?
  • 我想你可以跨函数使用 dplyr。
猜你喜欢
  • 2017-11-12
  • 2018-09-12
  • 2015-07-14
  • 2023-02-22
  • 2021-10-13
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多