I have data about a floodplain with a date and flooded 0/1. I have a funciton which counts the flooded days with different periods with different end date. Now I want to repeat the function for severeal columns (several heigths for one observation transect) and calculate the mean of flooded days for each period. I dont want to repeat my function for every column manual.
I thougt about a solution with a loop or something with the apply family, but I'm not enough R Guru.
set.seed(1)
df2 <- data.frame(date=seq(as.Date("2016-11-01"), as.Date("2018-11-01"), "day"),
flooded=rbinom(731, 1, .5), flooded2=rbinom(731, 1, .5), flooded2=rbinom(731, 1, .5))
date_end2 <- sort(sample(df2$date, 4))
period2 <- c(30,60,90)
###########################################################################################
floodCount <- function(datecol, floodcol, e, p) {
e <- as.Date(e)
datecol <- as.Date(datecol)
stopifnot(!anyNA(c(e, p)))
stopifnot((e - p) %in% datecol)
return(sum(floodcol[which((datecol == e - p + 1)):which(datecol == e)]))
}
F_2017 <- sapply(period2, function(p) with(df2, floodCount(date, flooded,date_end2[1], p)))
S_2017 <- sapply(period2, function(p) with(df2, floodCount(date, flooded,date_end2[2], p)))
F_2018 <- sapply(period2, function(p) with(df2, floodCount(date, flooded,date_end2[3], p)))
S_2018 <- sapply(period2, function(p) with(df2, floodCount(date, flooded,date_end2[4], p)))
FLOODED.T <- rbind(F_2017, S_2017, F_2018, S_2018)
FLOODED.T2 <- as.data.frame(FLOODED.T)
names(FLOODED.T2)[1:3] <- period2[1:3]
As solution of the intermediate step I expect a data.frame like this:
30 60 90 30_1 60_1 90_1 30_2 60_2 90_2 ...
F_2017 11 28 42 21 31 45 ...
S_2017 17 30 44 18 28 ...
F_2018 14 32 48 15 ...
S_2018 15 31 49 ...
As final output with the mean of each period per enddate
30_m 60_m 90_m
F_2017 10 30 52
S_2017 21 28 41
F_2018 13 32 47
S_2018 5 32 35
I'm open to your smart and genius R ideas ;)