Some sample data first
yr1 <- sample(0:1, 365, replace = T) yr2 <- sample(0:1, 365, replace = T) yr3 <- sample(0:1, 365, replace = T) yr4 <- sample(0:1, 365, replace = T) value <- c(yr1, yr2, yr3, yr4) yr <- rep(2000:2003, each = 365) doy <- rep(1:365, times = 4) foo <- as.data.frame(cbind(value, yr, doy)) foo contains 3 columns. Column 1 has arbitary value which is either 1 or 0. Column 2 contains year and column 3 has day of the year (365 days)
I have two vectors with start and end days in Julian days
start <- c(258, 258,258,258) mid <- c(279, 281,285,288) end <- c(286, 295,300,320) range.val <- as.data.frame(cbind(start, mid, end)) range.val$yr<- c(2000, 2001, 2002, 2003) range.val gives me the julian days between which I have to sum the values for each year in foo.
For example, for 2000, I need to sum foo$value starting from 258 day till 279 day and then from 279 till 286. Similarly, for 2001, sum foo$value from 258 till 281 and then from 281 till 295.
I also need to calculate length of the longest continous occurrence of 1 between these indices for each year.
I did this:
for(yr in 2000:2003){ range.sub <- range.val[range.val$yr == yr,] foo.sub <- foo[foo$yr == yr,] sum.1 <- sum(foo.sub[range.sub$start:range.sub$mid,"value"]) sum.2 <- sum(foo.sub[range.sub$mid:range.sub$end,"value"]) length.1 <- rle(foo.sub[range.sub$start:range.sub$mid,"value"]) max.spell.length <- max(sort(length.1$lengths, , decreasing = TRUE)) length.1 <- rle(foo.sub[range.sub$mid:range.sub$start,"value"]) max.spell.length1 <- max(sort(length.1$lengths, , decreasing = TRUE)) } In my continous effort to minimise the use of for-loop, I wonder if I can shorten the above code using some other function.