R 我想根据一个公共变量将一个数据帧中的值转换为另一个数据帧中的值

R 我想根据一个公共变量将一个数据帧中的值转换为另一个数据帧中的值,r,gis,R,Gis,我有两个数据帧。有几个目的地及其纬度和经度的一个。第二个具有行程数据,其中包括与第一个数据帧格式完全相同的始发地和目的地名称。我希望在第二帧中获得原点和终点,以获得相关坐标: > df <- data.frame(location = c("White House", "Impound Lot", "Bush Garden", "Rayburn", "Robertson House", "Beers Elementary"), latitude = c(38.89710, 38.81

我有两个数据帧。有几个目的地及其纬度和经度的一个。第二个具有行程数据,其中包括与第一个数据帧格式完全相同的始发地和目的地名称。我希望在第二帧中获得原点和终点,以获得相关坐标:

> df <- data.frame(location = c("White House", "Impound Lot", "Bush Garden", "Rayburn", "Robertson House", "Beers Elementary"), latitude = c(38.89710, 38.81289, 38.94178, 38.8867787, 38.9053894, 38.86466), longitude = c(-77.036545, -77.0171983, -77.073311, -77.0105317, -77.0616441, -76.95554))

> df2 <- data.frame(trip_id = c(1:8), trip_from = c("Impound Lot", "Bush Garden", "White House", "Robertson House", "Impound Lot", "Beers Elementary", "Bush Garden", "Rayburn"), trip_to = c("Bush Garden", "Impound Lot", "Rayburn", "Impound Lot", "Beers Elementary", "White House", "Impound Lot", "Bush Garden"))

> df
       location latitude longitude
1      White House 38.89710 -77.03655
2      Impound Lot 38.81289 -77.01720
3      Bush Garden 38.94178 -77.07331
4          Rayburn 38.88678 -77.01053
5  Robertson House 38.90539 -77.06164
6 Beers Elementary 38.86466 -76.95554

> df2
  trip_id        trip_from          trip_to
1       1      Impound Lot      Bush Garden
2       2      Bush Garden      Impound Lot
3       3      White House          Rayburn
4       4  Robertson House      Impound Lot
5       5      Impound Lot Beers Elementary
6       6 Beers Elementary      White House
7       7      Bush Garden      Impound Lot
8       8          Rayburn      Bush Garden
>df-df2-df
位置经纬度
1白宫38.89710-77.03655
2蓄水地块38.81289-77.01720
3布什花园38.94178-77.07331
4雷伯恩38.88678-77.01053
5罗伯逊之家38.90539-77.06164
6啤酒小学38.86466-76.95554
>df2
trip_id trip_从trip_到
1灌木丛花园蓄水地段
2灌木园蓄水场
3 3白宫雷伯恩
4罗伯逊大厦扣押地段
5.被扣押的一批啤酒
白宫啤酒小学
7灌木园蓄水场
雷伯恩布什花园8号

我希望有一个新的数据框,它获取
df
中每个位置的“纬度”和“经度”值,并将它们与
df2
中的每个位置相关联。因此,我希望有一个包含四个以上列的表(
lat\u from
lon\u from
lat\u to
lon\u to
),这样我就可以映射位置之间的流。

具有合并功能的解决方案:

 df3 <- merge(df2, df, by.x = "trip_to", by.y = "location")
merge(df3, df, by.x = "trip_from", by.y = "location", suffixes = c(".to", ".from"))

df3谢谢!这很有道理!