如何在R中计算中位数月份

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

How to compute the median month in R

问题

I have a dataframe df with several locations var1 and var2. For every year for these locations, the months where the temperature reaches an annual maximum maxmo or minimum minmo are given. I want to calculate the median month over the available years for which a certain location reaches an annual maximum or minimum.

  1. df <- data.frame(
  2. variable = c("var1","var1","var1","var1","var2", "var2","var2","var2"),
  3. year = c(2007:2010,2012:2015),
  4. maxmo = c(10,8,8,7,7,8,8,8),
  5. minmo=c(12,12,1,1,1,1,2,2))

I tried this

  1. df %>%
  2. group_by(variable)%>%
  3. summarize(maxmo2= median(maxmo), minmo2=median(minmo))

which gives me

  1. variable maxmo2 minmo2
  2. <chr> <dbl> <dbl>
  3. 1 var1 8 6.5
  4. 2 var2 8 1.5

while I want to get

  1. variable maxmo2 minmo2
  2. <chr> <dbl> <dbl>
  3. 1 var1 8 12.5
  4. 2 var2 8 1.5

or

  1. variable maxmo2 minmo2
  2. <chr> <dbl> <dbl>
  3. 1 var1 8 0.5
  4. 2 var2 8 1.5

So var1 situates between December and January (12.5 or 0.5)

英文:

I have a dataframe df with several locations var1 and var2. For every year for these locations the months where the temperature reaches an annual maximum maxmo or minimum minmo is given. I want to calculate the median month over the available years for which a certain location reaches an annual maximum or minimum.

  1. df <- data.frame(
  2. variable = c("var1","var1","var1","var1","var2", "var2","var2","var2"),
  3. year = c(2007:2010,2012:2015),
  4. maxmo = c(10,8,8,7,7,8,8,8),
  5. minmo=c(12,12,1,1,1,1,2,2))

I tried this

  1. df %>%
  2. group_by(variable)%>%
  3. summarize(maxmo2= median(maxmo), minmo2=median(minmo))

which gives me

  1. variable maxmo2 minmo2
  2. <chr> <dbl> <dbl>
  3. 1 var1 8 6.5
  4. 2 var2 8 1.5

while I want to get

  1. variable maxmo2 minmo2
  2. <chr> <dbl> <dbl>
  3. 1 var1 8 12.5
  4. 2 var2 8 1.5

or

  1. variable maxmo2 minmo2
  2. <chr> <dbl> <dbl>
  3. 1 var1 8 0.5
  4. 2 var2 8 1.5

So var1 situates between December and January (12.5 or 0.5)

答案1

得分: 4

将月份映射到24小时制并使用circular包:

  1. library(circular)
  2. library(dplyr)
  3. med <- function(x, eps = 0.01) {
  4. cir <- circular(2 * (x - 1), units = "hours")
  5. num <- as.numeric(median(cir)) %% 24
  6. ((num + (num > (24 - eps))) %% 24) / 2 + 1
  7. }
  8. df %>%
  9. group_by(variable) %>%
  10. summarize(across(starts_with("m"), med), .groups = "drop")

得到:

  1. # A tibble: 2 x 3
  2. variable maxmo minmo
  3. <chr> <dbl> <dbl>
  4. 1 var1 8 12.5
  5. 2 var2 8 1.5

只提供代码的翻译,不包括问题部分。

英文:

Map the months to a 24 clock and use the circular package:

  1. library(circular)
  2. library(dplyr)
  3. med &lt;- function(x, eps = 0.01) {
  4. cir &lt;- circular(2 * (x - 1), units = &quot;hours&quot;)
  5. num &lt;- as.numeric(median(cir)) %% 24
  6. ((num + (num &gt; (24 - eps))) %% 24) / 2 + 1
  7. }
  8. df %&gt;%
  9. group_by(variable) %&gt;%
  10. summarize(across(starts_with(&quot;m&quot;), med), .groups = &quot;drop&quot;)

giving

  1. # A tibble: 2 x 3
  2. variable maxmo minmo
  3. &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
  4. 1 var1 8 12.5
  5. 2 var2 8 1.5

huangapple
  • 本文由 发表于 2023年4月17日 21:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035666.html
匿名

发表评论

匿名网友

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

确定