【问题标题】:I want to take values from one data frame into a second based on a common variable我想根据一个公共变量将一个数据帧中的值转换为第二个数据帧
【发布时间】:2014-03-22 01:26:18
【问题描述】:

我有两个数据框。一个有多个目的地及其纬度和经度。第二个具有行程数据,其中包括与第一个数据帧完全相同格式的起点和目的地名称。我想在第二帧中获取起点和终点以关联坐标:

> 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)的表,以便映射位置之间的流。

【问题讨论】:

    标签: r gis


    【解决方案1】:

    合并的解决方案:

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

    【讨论】:

    • 谢谢!这很有道理!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2020-10-01
    • 2012-08-13
    • 1970-01-01
    相关资源
    最近更新 更多