我正在研究如何根据地址将颗粒物暴露分配给特定个体。我有两个数据集,其中一个包含个体的经度和纬度坐标,另一个则是颗粒物暴露块。我想基于最接近的颗粒物暴露块为每个主题分配颗粒物暴露块。
library(sp)
library(raster)
library(tidyverse)
#subject level data
subjectID<-c("A1","A2","A3","A4")
subjects<-data.frame(tribble(
~lon,~lat,
-70.9821391, 42.3769511,
-61.8668537, 45.5267133,
-70.9344039, 41.6220337,
-70.7283830, 41.7123494
))
row.names(subjects)<-subjectID
#PM Block Locations
blockID<-c("B1","B2","B3","B4","B5")
blocks<-data.frame(tribble(
~lon,~lat,
-70.9824591, 42.3769451,
-61.8664537, 45.5267453,
-70.9344539, 41.6220457,
-70.7284530, 41.7123454,
-70.7284430, 41.7193454
))
row.names(blocks)<-blockID
#Creating distance matrix
dis_matrix<-pointDistance(blocks,subjects,lonlat = TRUE)
###The above code doesnt preserve the row names. Is there a way to to do
that?
###I'm unsure about the below code
colnames(dis_matrix)<-row.names(subjects)
row.names(dis_matrix)<-row.names(blocks)
dis_data<-data.frame(dis_matrix)
###Finding nearst neighbor and coercing to usable format
getname <-function(x) {
row.names(dis_data[which.min(x),])
}
nn<-data.frame(lapply(dis_data,getname)) %>%
gather(key=subject,value=neighbor)
这段代码的输出结果看起来是有意义的,但我不确定它的有效性和效率。欢迎您提供改进和修复这段代码的建议。我还收到以下错误信息:
Warning message:
attributes are not identical across measure variables;
they will be dropped
我无法确定其来源。
感谢您的查看!
data.frame(subject=subjectID, block=blockID[r])
。 - Robert Hijmans