I am trying to calculate the annual total of monthly product(binary files) https://echange-fichiers.inra.fr/get?k=Jr3tvgeKnWUiC1B0MMY. So I have 12 files and I have to calculate the total sum. The code given below worked fine but what I need is to multiply each file by the number of days which correspond to that month in that year then calculate the total sum. For my case, those data are for the year 2000 (a leap year), so I want to multiply the results for file number 1 by 31 and file number 2 by 29 and so on....
files<- list.files("C:\\jung file_2000_img", "*.img", full.names = TRUE)
x<- do.call(rbind,(lapply(files, readBin , double() , size = 4 , n=360 * 720 , signed =TRUE)))
results <- colSums(x)
fileName <- sprintf("C:\\annual_ET2000_without999_1.img")
writeBin(as.double(results), fileName, size = 4)
From Jame's answer, this will calculate the number of days:
numDays <- function(month,year){
as.numeric(strftime(as.Date(paste(year+month%/%12,month%%12+1,"01",sep="-"))-1,"%d"))
}
So now how to apply that to my code above? By that I mean that I want to take the result for each file and multiply it by the number of days like:
file1*numDays(1,2000)+file2*numDays(2,2000)+file3*numDays(3,2000)
.............file12*numDays(12,2000)