我想将3个矩阵(lat,lon和obs)合并为一个唯一矩阵(或data.frame),其中每个值与相应的值匹配。
我们假设我有以下矩阵:
latitude_m
lat lat lat
lat 3 12 4
lat 5 45 9
lat 12 33 1
longitude_m
lon lon lon
lon 1 34 3
lon 23 5 1
lon 2 2 9
observations_m
obs obs obs
obs 45 13 6
obs 22 4 9
obs 2 1 10
我如何获得以下内容:
lat lon obs
3 1 45
5 23 22
12 2 2
12 34 13
45 5 4
33 2 1
4 3 6
9 1 9
1 9 10
由于
答案 0 :(得分:0)
Akruns解决方案效果很好,但是如果有一些与每个值的索引关联的信息,将它存储为数组可能是明智的。
set.seed(1)
lon <- matrix(sample(-180:180, 9), 3)
lat <- matrix(sample(-90:90, 9), 3)
obs <- matrix(round(runif(9)*9, 1), 3)
library(abind)
arr <- abind(lon, lat, obs, along=3)
如果您想将其折叠为二维,就像重置dim()
dim(arr) <- c(9, 3)
arr
# [,1] [,2] [,3]
# [1,] -85 -79 3.4
# [2,] -47 -53 7.0
# [3,] 25 -59 8.4
# [4,] 145 32 1.9
# [5,] -108 -23 5.9
# [6,] 139 45 1.1
# [7,] 155 -3 2.4
# [8,] 53 34 3.5
# [9,] 42 81 0.1