[R] how to calculate seasonal mean for temperatures

Rui Barradas ruipbarradas at sapo.pt
Wed Aug 1 15:10:30 CEST 2012


Hello,

See if this is it.


fun <- function(DF, FUN = mean){
     month <- as.integer(format(DF$date, format="%m"))
     year <- format(DF$date, format="%Y")
     month[month %in% 1:2] <- 13
     DF$season <- NA
     DF$season[month %in% 12:13] <- paste(year[month %in% 12:13], "Winter")
     DF$season[month %in% 3:5] <- paste(year[month %in% 3:5], "Spring")
     DF$season[month %in% 6:8] <- paste(year[month %in% 6:8], "Summer")
     DF$season[month %in% 9:11] <- paste(year[month %in% 9:11], "Fall")
     aggregate(data ~ season, data=DF, FUN = FUN)
}
fun(DF)


Hope this helps,

Rui Barradas

Em 01-08-2012 12:30, jeff6868 escreveu:
> Thank you both for your answers.
>
> I found a best way to delete the first 2 months (Jan + Feb) and the last
> month (Dec), which should work everytime:
>
> DF$year <- as.numeric(format(DF$Day, format = "%Y"))
> DF$month <- as.numeric(format(DF$Day, format = "%m"))
>
> # delete first 2 months
> for(i in DF[1,3])  # column year
>    	ifelse(i==2008,(DF= DF[c(-(1:60)),]),(DF=DF[c(-(1:59)),]))
> I delete the first 60 days if the first year if my file starts with a leap
> year (I'll need just to add the new leap years afterwords), and the 59 first
> days if it's not.
>
> # delete last month	
>      DF= DF[c(-((nrowDF)-30):nrow(DF))),]
>
> I did a mistake next for the seasons:
> - winter should be month 12,1,2 (so month 12 of the previous year, that's
> why I deleted the first 2 months)
> - spring: month 3,4,5
> - summer: month 6,7,8
> - autumn: month 9,10,11
>
> Now my file starts month 3 of the first year (as I deleted the first 2
> months).
> My first year has so: month 3,4,5,6,7,8,9,10,11,12 and the nest year: month
> 1,2,3,4,5,6,7,8,9,10,11 (we imagine a file with just 2 years).
>   I tried to modify your proposition but it doesn't work yet.
>
> Could you help me again with this new version of season?
> Thank you very much Ricardogg
>
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/how-to-calculate-seasonal-mean-for-temperatures-tp4638639p4638651.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list