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

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

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.

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

I tried this

df %>%
  group_by(variable)%>%
  summarize(maxmo2= median(maxmo), minmo2=median(minmo))

which gives me

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

while I want to get

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

or

  variable maxmo2 minmo2
  <chr>     <dbl>  <dbl>
1 var1          8    0.5
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.

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

I tried this

df %>%
  group_by(variable)%>%
  summarize(maxmo2= median(maxmo), minmo2=median(minmo))

which gives me

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

while I want to get

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

or

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

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

答案1

得分: 4

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

library(circular)
library(dplyr)

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

得到:

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

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

英文:

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

library(circular)
library(dplyr)

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

giving

# A tibble: 2 x 3
  variable maxmo minmo
  &lt;chr&gt;    &lt;dbl&gt; &lt;dbl&gt;
1 var1         8  12.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:

确定