I have 4 data frames, each data frame corresponding to a single year. Each data frame contains daily rainfall for five locations.
Generate sample data
location <- c("A","B","C","D","E")
mat <- round(as.data.frame(matrix(runif(1825),nrow=5,ncol=365)), digits=2)
dat.1981 <-as.data.frame(cbind(location,mat)) # rainfall for 1981
dat.1981$year <- 1981
mat <- round(as.data.frame(matrix(runif(1825),nrow=5,ncol=365)), digits = 2)
dat.1982 <- as.data.frame(cbind(location,mat)) # rainfall for 1982
dat.1982$year <- 1982
mat <- round(as.data.frame(matrix(runif(1825),nrow=5,ncol=365)), digits = 2)
dat.1983 <-as.data.frame(cbind(location,mat)) # rainfall for 1983
dat.1983$year <- 1983
mat <- round(as.data.frame(matrix(runif(1825),nrow=5,ncol=365)), digits = 2)
dat.1984 <-as.data.frame(cbind(location,mat)) # rainfall for 1984
dat.1984$year <- 1984
dat <- as.data.frame(rbind(dat.1981,dat.1982,dat.1983,dat.1984))
For each year, I want to classify whether a day was an extreme wet day or not
Here's how I do my calculation:
1) For each location, generate the mean and sd of rainfall for every week for the period 1981 to 1984. For example, in location A the mean rainfall for the first week will be:
(First week rain 1981 in A + First week rain 1982 in A + First week rain 1983 in A + First week rain 1984 in A)/4
which can be written in R as
mean.week1.loc1 <- mean(rowSums(dat[dat$location=="A",2:8])) # 2:8 selects the first 7 days in each year
sd.week1.loc1 <- sd(rowSums(dat[dat$location=="A",2:8]))
wet.cr <- mean.week1 + sd.week1 # this is my threshold for defining a wet day
If daily rainfall in week 1 for the years 1981 to 1984 in location A is greater than wet.cr
,
that day is a wet day and hence gets a value of 1
As an example, to examine whether rainfall of week 1 for location A for 1981 to 1984 is a wet day or not I can do the following:
lapply(dat[, 2:8], function(x) ifelse(x > wet.cr, 1, 0))
I want to repeat this for each week and each location.
However, I am unable to stitch these individual functions together and also
my final results should be a dataframe same as dat
but instead of rainfall values, I will have 1 or 0 defining whether it is a wet day or not.
EDIT
The solutions below gives me the following:
mean(c(rainfall 1981 day 1 week 1, ...., rainfall 1981 day 7 week 1, rainfall 1982 day 1 week 1,....,rainfall 1982 day 7 week 1,....,rainfall 1984 day 1 week 1,....,rainfall 1984 day 7 week 1))
WHAT I WANT IS:
mean(c(mean(total rainfall week 1 1981), mean(total rainfall week 1 1982), mean(total rainfall week 1 1983), mean(total rainfall week 1 1984)))
I hope this is clear now.
ISOweek
to grab the year and week, then just aggregate over year intidyr
. If you give an example of the data with full dates instead of broken down in columns by week, I can show you.... – sconfluentus