Reputation: 2533
I have [365 binary files][1]. I want to calculate the monthly average. So from the 365 files,
This code will take the average of every 30 files.
results <- list()
for (.files in files.group) {
x <- do.call(rbind,(lapply(.files, readBin , double() , size = 4, n =360 * 720,
signed =T)))
results[[length(results) + 1L]] <- colMeans(x)
}
I'd be grateful for any ideas on:
Upvotes: 2
Views: 1687
Reputation: 18749
dir1 <- "C:\\New folder (4)\\New folder"
files <- list.files(dir1, "*.bin",full.names=TRUE)
First you have to extract the number of the file (because they are not sorted the way you want them to be sorted (i. e. "ET10.bin" comes after "ET1.bin" and not "ET9.bin").
step1 <- strsplit(gsub("\\.bin","",files),split="ET")
filenumber <- do.call(rbind,step1)[,2]
Then this number is the day of the year in numerical form (recognized by strptime
as %j
). Let's say the year in question is 2012:
step2 <- strptime(paste(filenumber,"2012",sep="-"),format="%j-%Y")
files.group <- split(files, cut(step2, "month"))
Concerning the -999 values something like x[x == -999] <- NA
should do the trick as long as you remember excluding the NA values when computing your average values (i. e. colMeans(x, na.rm=TRUE)
)
Edit: As per @f3lix suggestion you can obtain filenumber
in a more straightforward way:
dir1 <- "C:\\New folder (4)\\New folder"
files <- list.files(dir1, "*.bin",full.names=TRUE)
filenumber <- gsub(".*ET([0-9]+).bin", "\\1", files)
files.group <- split(files, cut(strptime(paste(filenumber,"2012",sep="-"),format="%j-%Y"), "month"))
Then your loop:
results <- list()
for (i in 1:12) {
x <- do.call(rbind,(lapply(files.group[[i]], readBin ,
double() , size = 4, n =360 * 720, signed =T)))
x[x == -999] <- NA
results[[i]] <- colMeans(x, na.rm=TRUE)
}
for (i in seq_along(results)) {
fileName <- sprintf("C:\\Users\\New folder\\glo_%d.flt", i)
writeBin(as.double(results[[i]]), fileName, size = 4)
}
Upvotes: 4