[R] seasonal sum and mean and combine multiple, different data frames in .csv

arun smartpink111 at yahoo.com
Wed Feb 13 20:08:23 CET 2013


Hi Irucka,

No problem.

I guess this could be done using:
Sample data:
Dailydo<- structure(list(Date = structure(c(11231, 11232, 11233, 11234, 
11235, 11236, 11207, 11208, 11209, 11179, 11180, 11181, 11151, 
11152, 11123, 11093, 11064, 11065, 11035, 11036, 11008, 11009, 
10979, 10980, 11347, 11348, 11380, 11381, 11410, 11406, 11438, 
11439, 11470, 11502, 11528, 11560, 11561, 11593, 11594, 11621, 
11622, 11654, 11685, 11992, 11962, 11963, 11964, 11965, 11966, 
11937, 11938, 11939, 11909, 11910, 11911, 11881, 11882, 11853, 
11823, 11794, 11795, 11765, 11766, 11739, 11740, 11710, 11711, 
12077, 12078, 12110, 12111, 12140, 12136, 12168, 12169, 12200, 
12232, 12258, 12290, 12291, 12323, 12324, 12351, 12352, 12384, 
12415), class = "Date"), FluxDay = c(1512.2498, 1447.5309, 1410.6735, 
1366.4421, 1326.6835, 1300.182, 1248.2897, 1193.732, 1106.1205, 
1074.7132, 1037.1902, 1037.1479, 1104.1739, 1073.765, 1047.021, 
1136.9787, 988.592, 988.6488, 939.9868, 810.8965, 812.3177, 811.6353, 
801.9529, 842.2798, 812.4434, 792.9126, 823.3818, 738.851, 843.5701, 
782.4434, 792.9126, 783.3818, 793.851, 729.5701, 792.4434, 712.9126, 
793.3818, 783.851, 723.5701, 792.9126, 793.3818, 783.851, 743.5701, 
812.2498, 1437.5309, 1415.6735, 1368.4421, 1186.6835, 1188.182, 
1286.2897, 1147.732, 1186.1205, 987.7132, 1147.1902, 1137.1479, 
1204.1739, 973.765, 847.021, 1036.9787, 1088.592, 1188.6488, 
986.9868, 846.8965, 827.3177, 813.6353, 811.9529, 849.2798, 832.4434, 
782.9126, 835.3818, 745.851, 847.5701, 792.4434, 744.9126, 795.3818, 
976.851, 829.5701, 992.4434, 1112.9126, 893.3818, 783.851, 763.5701, 
798.9126, 795.3818, 763.851, 783.5701)), .Names = c("Date", "FluxDay"
), row.names = c(NA, -86L), class = "data.frame")

str(Dailydo)
#'data.frame':    86 obs. of  2 variables:
# $ Date   : Date, format: "2000-10-01" "2000-10-02" ...
# $ FluxDay: num  1512 1448 1411 1366 1327 ...


DailydoSum<-funSumSeason(Dailydo)# your function
DailydoSum
#        season  FluxDay
#1    2000 Fall 8363.762
#2  2000 Spring 4161.240
#3  2000 Summer 8875.132
#4  2000 Winter 5019.069
#5    2001 Fall 3113.716
#6  2001 Spring 3099.716
#7  2001 Summer 3806.159
#8  2001 Winter 4793.602
#9    2002 Fall 7408.762
#10 2002 Spring 4161.240
#11 2002 Summer 9070.132
#12 2002 Winter 5136.069
#13   2003 Fall 3141.716
#14 2003 Spring 3346.716
#15 2003 Summer 4546.159
#16 2003 Winter 4836.602


Suppose if I add 1 to each of Fall Season year:
DailydoSum$season[grep("Fall",DailydoSum$season)]<- paste(as.numeric(gsub("\\D+","",DailydoSum$season[grep("Fall",DailydoSum$season)]))+1,"Fall")
DailydoSum
#        season  FluxDay
#1    2001 Fall 8363.762
#2  2000 Spring 4161.240
#3  2000 Summer 8875.132
#4  2000 Winter 5019.069
#5    2002 Fall 3113.716
#6  2001 Spring 3099.716
#7  2001 Summer 3806.159
#8  2001 Winter 4793.602
#9    2003 Fall 7408.762
#10 2002 Spring 4161.240
#11 2002 Summer 9070.132
#12 2002 Winter 5136.069
#13   2004 Fall 3141.716
#14 2003 Spring 3346.716
#15 2003 Summer 4546.159
#16 2003 Winter 4836.602


#split based on year
sapply(split(DailydoSum, as.numeric(gsub("\\D+","",DailydoSum$season))), function(x)  Sum=sum(x[,2]))
 #    2000      2001      2002      2003      2004 
#18055.442 20063.238 21481.157 20138.238  3141.716 

In this dataset, some year doesn't have all the Seasons, so the value is less.

I hope it hleps.
A.K.




----- Original Message -----
From: "iruckaE at mail2world.com" <iruckaE at mail2world.com>
To: smartpink111 at yahoo.com
Cc: 
Sent: Wednesday, February 13, 2013 1:27 PM
Subject: Re: seasonal sum and mean and combine multiple, different data frames in .csv

Hi Arun, thank-you for your assistance.

The code that you suggested is useful to calculate the sum of the calendar year, but I need to be able to also sum the water year as well (October to September).

For example, the sum of 2000 Fall, 2001 Winter, 2001 Spring, and 2001 Summer would represent the sum for the 2001 water year.

Can you offer any advice on summing up the seasons from October of the preceding year to September of the next year?

funMeanSeason <- function(DF, FUN = mean){
month <- as.integer(format(DF$Date, format="%m"))
year <- format(DF$Date, format="%Y")
DF$season<- NA
DF$season[month %in% 10:12] <- paste(year[month %in% 10:12], "Fall")
DF$season[month %in% 1:3] <- paste(year[month %in% 1:3], "Winter")
DF$season[month %in% 4:6] <- paste(year[month %in% 4:6], "Spring")
DF$season[month %in% 7:9] <- paste(year[month %in% 7:9], "Summer")
aggregate(FluxDay ~ season, data=DF, FUN = FUN) # changed here
}

ju <- funMeanSeason(Dailydo) # this provides the seasonal mean


funSumSeason <- function(DF, FUN = sum){
month <- as.integer(format(DF$Date, format="%m"))
year <- format(DF$Date, format="%Y")
DF$season<- NA
DF$season[month %in% 10:12] <- paste(year[month %in% 10:12], "Fall")
DF$season[month %in% 1:3] <- paste(year[month %in% 1:3], "Winter")
DF$season[month %in% 4:6] <- paste(year[month %in% 4:6], "Spring")
DF$season[month %in% 7:9] <- paste(year[month %in% 7:9], "Summer")
aggregate(FluxDay ~ season, data=DF, FUN = FUN) # changed here
}

ju1 <- funSumSeason(Dailydo) # this provides the seasonal sum

ju2 <- sapply(split(ju1[,-1],as.numeric(gsub("\\D+","",ju1$season))),sum) # this provides the yearly sum for the calendar year, but I need it for a water year (see above)

Thank-you.

Irucka




More information about the R-help mailing list