英文:
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 <- 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")
giving
# A tibble: 2 x 3
variable maxmo minmo
<chr> <dbl> <dbl>
1 var1 8 12.5
2 var2 8 1.5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论