我有一个四维的水温数组[lon,lat,dept,done],我想基于一个矩阵提取月温度,该矩阵中的行和列分别为lon和lat,并指定深度级别。结果将是可变深度[lon,lat,month]的每月温度的3维数组。
仅提供一个示例:
set.seed(1)
temperature <- array(rnorm(10), rep(3,4))
depth <- matrix(c(NA, sample(1:3, 8, replace = TRUE)), 3, 3)
我看了如何动态索引多维R数组?和R使用矩阵选择多维数组的行,但无法解决该怎么做?
任何帮助深表感谢。
可以相距很远,但是可以尝试一下:
#get the indices relative to lon, lat and depth
threeIndices<-cbind(c(row(depth)),c(col(depth)),c(depth))
#repeat each row of the above to host a different month value
threeIndices<-threeIndices[rep(1:nrow(threeIndices),dim(temperature)[4]),]
#define the month index
fourthIndex<-rep(1:dim(temperature)[4],each=nrow(threeIndices)/dim(temperature)[4])
#putting all together
allIndices<-cbind(threeIndices,fourthIndex)
#subsetting and putting in an array
array(temperature[allIndices],dim(temperature)[c(1,2,4)])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句