【问题标题】:How to calculate average of values from specific days each year for multiple years in R?如何计算R中多年每年特定日期的平均值?
【发布时间】:2020-07-05 22:29:02
【问题描述】:

我想计算每年特定时间段的平均温度 (t)。

我有天气数据,可以为我提供每一天的价值。我的真实数据是从 2011 年到 2019 年,并且所有年份都有所有的日子,我想例如每年 4 月 20 日至 5 月 15 日的平均温度。

示例数据:

df <- data.frame(matrix(ncol = 4, nrow = 8))
x <- c("year", "month","day","t")
colnames(df) <- x
df$year <- c(2011,2011,2011,2011,2012,2012,2012,2012)
df$month <- c(3,3,4,4,3,3,4,4)
df$day <- c(1,2,3,4,1,2,3,4)
df$t <- c(1,3,6,1,2,7,1,-9)

我确实设法用一个非常丑陋且耗时的代码来做到这一点,但缺乏知识阻止了我的前进。

提前谢谢你。

【问题讨论】:

  • 不要将数据共享为图像,而是使用 dput() 创建可重现的示例。更多想法在这里:stackoverflow.com/questions/5963269/…
  • 此外,您实际尝试了什么?
  • 我确实使用了 as.date 并使用“%Y-%m-%d”创建了一个列,并选择了每个时间段并手动取平均值并将其混合到一个日期帧中。我是初学者,所以这既费时又非常丑陋的代码。我正在寻找更好的方法。欢迎所有帮助。

标签: r date weather


【解决方案1】:

使用tidyverse,您可以做类似的事情:

library(tidyverse)

Data %>%
  filter((month == 4 & day >= 20) |
         (month == 5 & day <= 15)) %>%
  group_by(year) %>%
  summarise(mean_temp = mean(t))

【讨论】:

    【解决方案2】:

    类似于@Ben 的答案,但在基础 R 中:

    aggregate(t~year, subset(df, (month == 4 & day >= 20) | 
                                 (month == 5 & day <= 15)), mean)
    

    【讨论】:

      【解决方案3】:

      您实际上可以在dplyr 包中的group_by 函数中添加相当复杂的计算。也许你想研究这样的事情。

      library(dplyr)
      library(lubridate)
      df <- data.frame(matrix(ncol = 4, nrow = 8))
      
      x <- c("year", "month","day","t")
      colnames(df) <- x
      df$year <- c(2011,2011,2011,2011,2012,2012,2012,2012)
      df$month <- c(3,3,4,4,3,3,4,4)
      df$day <- c(1,2,3,4,1,2,3,4)
      df$t <- c(1,3,6,1,2,7,1,-9)
      df %>% 
        group_by(lubridate::dmy(paste(day, month, year)) %>% 
                   lubridate::yday() %>% 
                   between(lubridate::yday(dmy("3.4.2000")), lubridate::yday(dmy("15.5.2000")))) %>% 
        summarise(mean(t)) 
      

      我正在使用 lubridate 的 yday 函数来选择多年的日期。

      希望这会有所帮助!

      【讨论】:

        【解决方案4】:

        试试下面的代码,我喜欢用for循环来处理这种麻烦。

        # Create a vector of all years
        year_u <- unique(zz$year)
        
        # Create the initial and final period
        inicial_day <- 20
        inicial_month <- 4
        
        final_day <- 15
        final_month <- 5
        
        # Create an empty data.frame to store the data after each loop
        averages <- data.frame()
        
        # Open a loop
        for(i in 1:length(year)){
        
            # take each year
            subsets <- subset(zz, year == year_u[i])
        
            # Mean of each time between the period
            average <- mean(subsets[subsets$day >= inicial_day & subsets$month >= inicial_month &
                                        subsets$day <= final_day & subsets$month <= final_month, ]$t)
        
            # Create a temporary data.frame to store the year and the t_mean
            temp <- data.frame(year = year_u[i], t_mean = average)
        
            # Combine the actual data with the last
            averages <- rbind(averages, temp)
        }
        

        【讨论】:

        • 最后,data.frame 平均值将有年份和周期的 t_means。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-22
        • 2021-11-01
        • 2020-07-27
        • 1970-01-01
        • 1970-01-01
        • 2018-07-01
        • 1970-01-01
        相关资源
        最近更新 更多