Having searched for similar posts, I am posting my question. I have monthly rainfall variables for several years for each site. I need to calculate monthly average rainfall over the years. I have given a simple data frame as follows. I need to create a new data frame that consists of monthly averages (12) for each site.
d<-structure(list(ID = structure(1:4, .Label = c("A", "B", "C",
"D"), class = "factor"), X2000_1 = c(25L, 42L, 74L, 52L), X2000_2 = c(15L,
15L, 51L, 12L), X2000_3 = c(14L, 21L, 25L, 41L), X2000_4 = c(74L,
4L, 23L, 51L), X2000_5 = c(15L, 25L, 65L, 12L), X2000_6 = c(31L,
23L, 15L, 25L), X2001_1 = c(52L, 54L, 18L, 63L), X2001_2 = c(85L,
165L, 12L, 12L), X2001_3 = c(25L, 36L, 20L, 14L), X2001_4 = c(1L,
17L, 23L, 52L), X2001_5 = c(24L, 45L, 12L, 15L), X2001_6 = c(3L,
23L, 45L, 52L)), .Names = c("ID", "X2000_1", "X2000_2", "X2000_3",
"X2000_4", "X2000_5", "X2000_6", "X2001_1", "X2001_2", "X2001_3",
"X2001_4", "X2001_5", "X2001_6"), class = "data.frame", row.names = c(NA,
-4L))
The output should be like;
df<-data.frame(id = c("A","B","C","D"))
df[c("jan","feb","mar","apr","may","jun")]<-NA
for example the cell A1 should contains the average rainfall of X2000_1 and X2001_1
I tried my codes as below but it does not work may be because I am using data frame. Any help would be much appreciated.
n = 6
unname(tapply(d, (seq_along(d)-1) %/% n, sum))
The column names of my actual data frame are
c("est", "X1990_1", "X1990_2", "X1990_3", "X1990_4", "X1990_5",
"X1990_6", "X1990_7", "X1990_8", "X1990_9", "X1990_10", "X1990_11",
"X1990_12", "X1991_1", "X1991_2", "X1991_3", "X1991_4", "X1991_5",
"X1991_6", "X1991_7", "X1991_8", "X1991_9", "X1991_10", "X1991_11",
"X1991_12", "X1992_1", "X1992_2", "X1992_3", "X1992_4", "X1992_5",
"X1992_6", "X1992_7", "X1992_8", "X1992_9", "X1992_10", "X1992_11",
"X1992_12", "X1993_1", "X1993_2", "X1993_3", "X1993_4", "X1993_5",
"X1993_6", "X1993_7", "X1993_8", "X1993_9", "X1993_10", "X1993_11",
"X1993_12", "X1994_1", "X1994_2", "X1994_3", "X1994_4", "X1994_5",
"X1994_6", "X1994_7", "X1994_8", "X1994_9", "X1994_10", "X1994_11",
"X1994_12", "X1995_1", "X1995_2", "X1995_3", "X1995_4", "X1995_5",
"X1995_6", "X1995_7", "X1995_8", "X1995_9", "X1995_10", "X1995_11",
"X1995_12", "X1996_1", "X1996_2", "X1996_3", "X1996_4", "X1996_5",
"X1996_6", "X1996_7", "X1996_8", "X1996_9", "X1996_10", "X1996_11",
"X1996_12", "X1997_1", "X1997_2", "X1997_3", "X1997_4", "X1997_5",
"X1997_6", "X1997_7", "X1997_8", "X1997_9", "X1997_10", "X1997_11",
"X1997_12", "X1998_1", "X1998_2", "X1998_3", "X1998_4", "X1998_5",
"X1998_6", "X1998_7", "X1998_8", "X1998_9", "X1998_10", "X1998_11",
"X1998_12", "X1999_1", "X1999_2", "X1999_3", "X1999_4", "X1999_5",
"X1999_6", "X1999_7", "X1999_8", "X1999_9", "X1999_10", "X1999_11",
"X1999_12", "X2000_1", "X2000_2", "X2000_3", "X2000_4", "X2000_5",
"X2000_6", "X2000_7", "X2000_8", "X2000_9", "X2000_10", "X2000_11",
"X2000_12")
You can extract the months as variable from the column names, and split the data frame as a list by the months variable and for each sub data frame calculate the row average with the
rowMeans()
function: