Annual, monthly or daily mean for irregular time series

前端 未结 4 1147
梦如初夏
梦如初夏 2020-12-16 07:53

I am a new user of \"R\", and I couldn\'t find a good solution to solve it. I got a timeseries in the following format:

>dates  temperature depth   salini         


        
4条回答
  •  失恋的感觉
    2020-12-16 08:11

    Yet, another method using plyr:

    df <- structure(list(dates = c("12/03/2012 11:26", "12/03/2012 11:56", 
       "12/03/2012 12:26"), temperature = c(9.7533, 9.6673, 9.6673), 
       depth = c(0.48073, 0.33281, 0.33281), salinity = c(37.607, 
       37.662, 37.672)), .Names = c("dates", "temperature", "depth",                                                                                                
      "salinity"), row.names = c(NA, -3L), class = "data.frame")
    
    library(plyr)
    
    # Change date to POSIXct
    df$dates <- with(d,as.POSIXct(dates,format="%m/%d/%Y %H:%M"))
    
    # Make new variables, year and month
    df <- transform(d,month=as.numeric(format(dates,"%m")),year=as.numeric(format(dates,"%Y")))
    
    ## According to year
    ddply(df,.(year),summarize,meantemp=mean(temperature),meandepth=mean(depth),meansalinity=mean(salinity))
      year meantemp meandepth meansalinity
    1 2012 9.695967 0.3821167       37.647
    
    ## According to month
    ddply(df,.(month),summarize,meantemp=mean(temperature),meandepth=mean(depth),meansalinity=mean(salinity))
      month meantemp meandepth meansalinity
    1    12 9.695967 0.3821167       37.647
    

提交回复
热议问题