简体   繁体   English

如何从R中的每日文件计算月平均值?

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

I have [365 binary files][1]. 我有[365个二进制文件] [1]。 I want to calculate the monthly average. 我想计算每月平均值。 So from the 365 files , 因此,从365个文件中

This code will take the average of every 30 files. 该代码平均每30个文件一次。

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: 对于以下任何想法,我将不胜感激:

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 (ie "ET10.bin" comes after "ET1.bin" and not "ET9.bin"). 首先,您必须提取文件的编号(因为它们未按照您希望的排序方式进行排序(即,“ ET10.bin”位于“ ET1.bin”之后,而不是“ 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 ). 然后,此数字是数字形式的一年中的某天(被strptime识别为%j )。 Let's say the year in question is 2012: 假设有问题的年份是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 (ie colMeans(x, na.rm=TRUE) ) 关于-999值,例如x[x == -999] <- NA colMeans(x, na.rm=TRUE)应该可以解决问题,只要您记得在计算平均值时排除NA值即可( colMeans(x, na.rm=TRUE)

Edit : As per @f3lix suggestion you can obtain filenumber in a more straightforward way: 编辑 :根据@ f3lix建议,您可以以更直接的方式获取filenumber

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)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM