我想基于公共变量将值从一个数据帧中取出来

时间:2014-02-20 20:39:08

标签: r gis

我有两个数据框。一个有几个目的地和他们的经度和纬度。第二个具有行程数据,其中包括与第一个数据帧一样的确切格式的起始和目的地名称。我想在第二帧中获得原点和目的地以获得相关的坐标:

> 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中的每个位置相关联。因此,我希望有一个包含四列(lat_fromlon_fromlat_tolon_to)的表格,以便我可以在不同位置之间映射流量。

1 个答案:

答案 0 :(得分:0)

合并解决方案:

 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"))