Get monthly means from dataframe of several years of daily temps

后端 未结 3 624
星月不相逢
星月不相逢 2020-12-04 00:43

I have daily temperature values for several years, 1949-2010. I would like to calculate monthly means. Here is an example of the data:

head(tmeasmax)
TIMESTE         


        
相关标签:
3条回答
  • 2020-12-04 01:16

    Here's a way to do it with the dplyr package:

    library(dplyr)
    library(lubridate)
    
    tmeasmax$TIMESTEP = ymd(tmeasmax$TIMESTEP)
    
    tmeasmax %>% 
      group_by(Year=year(TIMESTEP), Month=month(TIMESTEP)) %>%
      summarise(meanDailyMin=mean(MINIMUM.C.),
                meanDailyMean=mean(MEAN.C.))
    
      Year Month meanDailyMin meanDailyMean
    1 1949     1       11.095      11.71928
    

    You can summarise any other column by month in a similar way.

    0 讨论(0)
  • 2020-12-04 01:19

    Here's a quick data.table solution. I assuming you want the means of MEAN.C. (?)

    library(data.table)
    setDT(tmeasmax)[, .(MontlyMeans = mean(MEAN.C.)), by = .(year(TIMESTEP), month(TIMESTEP))]
    #    year month MontlyMeans
    # 1: 1949     1    11.71928
    

    You can also do this for all the columns at once if you want

    tmeasmax[, lapply(.SD, mean), by = .(year(TIMESTEP), month(TIMESTEP))]
    #    year month  MEAN.C. MINIMUM.C. MAXIMUM.C. VARIANCE.C.2. STD_DEV.C.      SUM COUNT
    # 1: 1949     1 11.71928     11.095   12.64667     0.2942481   0.482513 1.426652     6
    
    0 讨论(0)
  • 2020-12-04 01:21

    You can use the lubridate package to create a new factor variable consisting of the year-month combinations, then use aggregate.

    library('lubridate')
    
    tmeasmax2 <- within(tmeasmax, {
            monthlies <- paste(year(TIMESTEP),
                               month(TIMESTEP))
    })
    
    aggregate(tmeasmax2, list(monthlies), mean, na.rm = TRUE)
    
    0 讨论(0)
提交回复
热议问题