【发布时间】:2020-07-19 14:51:11
【问题描述】:
有人可以帮助我吗?以下是详细信息。
示例数据框 1:
Latitude Longitude
12.10 4.10
12.20 4.20
12.30 4.50
数据框 2:
ID Latitude1 Latitude2 Longitude1 Longitude2
ABC 11.5 12.15 3.9 4.15
DEF 12.17 12.25 4.17 4.25
GHI 12.27 12.45 4.45 4.48
所需的输出:
Latitude Longitude ID
12.10 4.10 ABC
12.20 4.20 DEF
12.30 4.50 NA
输出中的第 3 行是 NA,因为它的经度值不在 dataframe2 给定的范围之间。
尝试的解决方案: 我创建了一个函数并使用了 DPLYR,但我只能对一个向量(纬度)进行范围查找。
getValue <- function(x,data) {
tmp <- data %>%
filter(Latitude1 <= x, x <= Latitude2) %>%
filter(row_number() == 1)
return(tmp$ID)
}
data_interval <- sapply(df1$Latitude, getValue, data=df2)
df1 输入:
df1 <- structure(list(Latitude = c(12.1, 12.2, 12.3), Longitude = c(4.1,
4.2, 4.5)), row.names = c(NA, -3L), class = c("tbl_df", "tbl",
"data.frame"))
df2 输入:
df2 <- structure(list(ID = c("ABC", "DEF", "GHI"), Latitude1 = c(11.5,
12.17, 12.27), Latitude2 = c(12.15, 12.25, 12.45), Longitude1 = c(3.9,
4.17, 4.45), Longitude2 = c(4.15, 4.25, 4.48)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))
【问题讨论】:
-
感谢@Allan Cameron 的编辑,我是新成员。
标签: r dplyr data-science data-cleaning