我一直在寻找很长时间来找出如何通过 R 计算空间平均值。但是,我找不到东西。我有一个二进制文件,有 720 列和 360 行,气温浮动。我想首先计算整个区域的空间平均值并计算(500-600 colu 和 200-250rows)区域的空间平均值,然后提取该区域。
t<- file("C:annual_Prc2000_without999_1.img","rb")
e=readBin(t, double(), size=4,n=720*360, signed=TRUE)
从答案1:
mean(e)
I got
NaN
从答案 2:我得到
spat_mean = apply(e, c(2,1), mean)
Error in apply(e, c(2, 1), mean) : dim(X) must have a positive length
让我解释一下:假设
m <- matrix(rnorm(10000),4,4)
> m
[,1] [,2] [,3] [,4]
[1,] 0.7930132 2.1973895 0.063493345 -0.5484056
[2,] 0.5222513 0.4331308 -0.002393336 0.1725495
[3,] 1.7462222 -1.5701996 -2.276781240 0.5628531
[4,] -1.2713361 -0.9349057 0.757412225 1.5118180
> mean(m)
[1] 0.134757
now I want this:
[,1] [,2] [,3] [,4]
[1,] 0.134757 0.134757 0.134757 0.134757
[2,] 0.134757 0.134757 0.134757 0.134757
[3,] 0.134757 0.134757 0.134757 0.134757
[4,] 0.134757 0.134757 0.134757 0.134757
我手动执行此操作,但我想通过 R 执行此操作