我有两个数据框df1
和df2
(见下文)
df1<-structure(list(Lat = c(-89.75, -89.25, -88.75, -88.25, -87.75,
-87.25, -86.75, -86.25, -85.75, -85.25), Long = c(-179.75, -179.25,
-178.75, -178.25, -177.75, -177.25, -176.75, -176.25, -175.75,
-175.25), NOy = c(3.91364542905387e-15, 3.91952637880666e-15,
3.92540732855945e-15, 3.93128785479576e-15, 3.93716880454855e-15,
3.93803955441832e-15, 3.93390010440509e-15, 3.92976065439186e-15,
3.92562120437863e-15, 3.9214817543654e-15), NHx = c(1.14955574068474e-15,
1.1571213330905e-15, 1.16468681961713e-15, 1.17225241202289e-15,
1.17981800442865e-15, 1.18044237358989e-15, 1.17412562538574e-15,
1.1678088771816e-15, 1.16149212897745e-15, 1.1551753807733e-15
)), .Names = c("Lat", "Long", "NOy", "NHx"), row.names = c(NA,
10L), class = "data.frame")
和
df2<- structure(list(x = c(148.1516, 4.5206, 5.9981, -51.4500008, -125.3336,
-125.2909, -124.9004, -82.1556, -98.4808, -98.4839), y = c(-35.6566,
51.3092, 50.3051, -1.72000003, 49.8673, 49.8705, 49.5346, 48.2167,
55.8796, 55.8792)), .Names = c("x", "y"), row.names = c(NA, 10L
), class = "data.frame")
两个数据帧包含lat和long坐标。我想从df1导出值NOy和NHx,它不知何故在df2的坐标内。输出将是一个数据帧,其中包含df2的坐标值以及从df1导出的各自的NOy和NHx值。 df3
显示输出的外观。 NA值显然应该是从df1而不是NA输出的实际数据。
df3<- structure(list(x = c(148.1516, 4.5206, 5.9981, -51.4500008, -125.3336,
-125.2909, -124.9004, -82.1556, -98.4808, -98.4839), y = c(-35.6566,
51.3092, 50.3051, -1.72000003, 49.8673, 49.8705, 49.5346, 48.2167,
55.8796, 55.8792), NOy = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), NHx = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("x",
"y", "NOy", "NHx"), row.names = c(NA, 10L), class = "data.frame")
任何人都知道怎么做?
答案 0 :(得分:0)
df1
最初来自ncdf文件,因此我为每个变量NOy和NHx创建了一个栅格对象:
library(raster)
NOy_Raster <- raster("file.nc", varname="NOy_deposition")
NHx_Raster <- raster("file.nc", varname="NHx_deposition")
然后我用数据框df2
中的点坐标提取了NOy和NHx的值:
df3 <- data.frame(coordinates(df2),
df2$x,
extract(NOy_Raster, df2),
extract(NHx_Raster, df2))