[R] How to calculate monthly average from daily files in R?
Pascal Oettli
kridox at ymail.com
Fri Jan 18 10:11:31 CET 2013
Hello,
I guess that you are working on Windows. So, I don't know whether the
following is pertinent or not.
On Linux, the first file is 'ET100.bin', not 'ET1.bin'. Thus, the first
'monthly' mean is calculated between April, 10 and May, 10.
You can try the following script. When I read your data, the code for
the missing values is "-999", not "-32765", maybe due to the machine.
You can manage your own missing value code by modifying "missval".
#-------------------------------------------------------------------------
missval <- -999
files <- paste0("C:\\New folder (4)\\New folder\\ET",1:365,".bin")
mm <- factor(rep(1:12, c(31,28,31,30,31,30,31,31,30,31,30,31)))
files.group <- split(files, mm)
monthly <- NULL
for(ff in files.group){
x <- do.call(rbind,(lapply(ff, readBin, double(), size=4, n=360*720,
signed=T)))
x[x==missval] <- NA
x <- apply(x,2,mean)
monthly <- rbind(monthly, x)
rm(x)
}
dim(monthly)
#-------------------------------------------------------------------------
HTH,
Pascal
Le 18/01/2013 02:37, Jonsson a écrit :
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list