我有一个数据集,其中包含所有美国城市的纬度和经度。我想将纬度和经度分配给df。但是,有多个城市有不同的国家。如何将Lat和Lon分配到正确的城市和州?
Cities <- data.frame(City = c("Sunnyvale", "Sunnyvale","Sunnyvale","Sunnyvale"),
State = c("CA","MO", "NC", "TX"),
Lat = c(37.36889, 37.05083, 35.73889, 32.79639 ),
Lon = c(-122.03528, -94.495, -82.13611, -96.56056))
df <- data.frame(City2 = c("Sunnyvale", "Sunnyvale"),
State2 = c("CA", "NC"))
答案 0 :(得分:0)
我们可以在“州”和“城市”列上使用join
library(data.table)
setDT(df)[Cities, c("Lat", "Lon") := .(Lat, Lon), on = .(City2 = City, State2 = State)]
df
# City2 State2 Lat Lon
#1: Sunnyvale CA 37.36889 -122.03528
#2: Sunnyvale NC 35.73889 -82.13611
答案 1 :(得分:0)
使用left_join
&#39; s > library(dplyr)
> left_join(df, Cities, by = c("City2" = "City", "State2" = "State"))
City2 State2 Lat Lon
1 Sunnyvale CA 37.36889 -122.03528
2 Sunnyvale NC 35.73889 -82.13611
:
Android Plugin for Gradle 3.0.0-beta3