[R] How to calculate monthly average from daily files in R?

Jonsson amen.alyaari at Bordeaux.inra.fr
Thu Jan 17 18:37:40 CET 2013


I have 365 binary files:  
https://echange-fichiers.inra.fr/get?k=oy3CN1yV1Um7ouRWm2U   ,I want to
calculate the monthly average. So from the 365 files, I will get 12 files.I
would like also to tell R not to take into account the no-data value
(-32765).for example, for the first month, there are 31 records: 3 of these
records has the value -32765,I want R to take the average of the rest
records(28  records) and so on with all months.

This code will take the average of every 30 files(any idea on how to make it
according to number of days in a month?and not to take into account the
no-data values)

           files<- list.files("C:\\New folder (4)\\New folder",
"*.bin",full.names=TRUE)
               # assume that we want to take the average of every 30 files
            files.group<- split(files , rep(seq_along(files), each = 30,
length =length(files)))
                  results<- list()
                 for (.files in files.group){
               # read in the 30 files as a vector of numbers that you take
the average of
             x<- do.call(rbind,(lapply(.files, readBin  , double() , size =
4 ,n =360 * 720 , signed =T)))
              ## take the means across the 30 files
             results[[length(results) + 1L]]<- colMeans(x)}
              close(x)
             for (i in seq_along(results)){
            fileName <- sprintf("C:/New folder/glo_%d.flt", i)
            writeBin(as.double(results[[i]]), fileName, size = 4)}



--
View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-monthly-average-from-daily-files-in-R-tp4655869.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list