###############################################
require(reshape2)
###############################################
#read csv file with rain BoM data
dat = read.csv('raj_rain_data.csv')
fn <- apply(dat[,2:13], 2, ecdf) # equivalent to Excel's percentrank function, only for cols 2 to 13 but you need to apply the function to each month
#this fun does all the months at once
fn2 = data.frame(t(do.call("rbind", sapply(1:12, FUN = function(i) fn[[i]](dat[,i+1]), simplify = FALSE))))
colnames(fn2) = colnames(dat[,2:13])
fn2$year = dat$Year
#if the value is lower than 0.4, retrieves a 1, otherwise 0
fn2$drought = rowSums(ifelse(apply(fn2[,1:12], 2, FUN= function (x) x < 0.4)==TRUE, 1,0))
#if the value is lower than 0.1, retrieves a 1, otherwise 0
fn2$extreme = rowSums(ifelse(apply(fn2[,1:12], 2, FUN= function (x) x < 0.1)==TRUE, 1,0))
str(fn2)
names(fn2)
#ts didn't work - Francis suggested to melt the data.frame # Melt
#fn2 to tall data set
fn2.tall <- melt(fn2, id.vars="year")
# Get the year-month into date format
fn2.tall$date <- with(fn2.tall,
as.Date(paste("1", variable, year), "%d %b %Y"))
# Convert dates into months since 0 BC/AD (arbitrary, but doesn't
# matter)
#I'm not using the month.idx with Ivan's solution (remove)
fn2.tall$mnth.idx <- sapply(fn2.tall$date, function(x){
12*as.integer(format(x, "%Y")) + (as.integer(format(x, "%m")) - 1)
})
# Sort by date
fn2.tall <- fn2.tall[order(fn2.tall$date),]
#Ivan's solution
x<-fn2.tall$value<0.4
xx <- (cumsum(!x) + 1) * x
x2<-(seq_along(x) - match(xx, xx) + 1) * x
fn2.tall$count<-x2
#counts the number of cases of drought
as.data.frame(table(fn2.tall$count))
###############################################
###############################################