是否可以关联两个数据框(例如:国家和城市)?

时间:2016-10-10 17:42:11

标签: r dataframe

我有三个数据帧:

cities_df,其中包含其他字段中的城市名称

cities_df <- data.frame(
  city_name = c("London", "Newcastle Upon Tyne", "Gateshead"),
  city_population = c(8673713L, 289835L, 120046L),
  city_area = c(1572L, 114L, NA)
)

states_df,其中包含其他字段中的州名称

states_df <- data.frame(
  state_name = c("Greater London", "Tyne and Wear"),
  state_population = c(123, 456)
)

dictionary_df,其中包含整个城市列表及其对应的状态。

dictionary_df <- data.frame(
  city_name = c("London", "Newcastle Upon Tyne", "Gateshead"),
  state = c("Greater London", "Tyne and Wear", "Tyne and Wear")
)

有没有办法关联/链接cities_dfstates_df数据框,以便我可以轻松获取属于某个州的所有城市字段?

1 个答案:

答案 0 :(得分:1)

使用合并,有关更多选项,请参阅linked post

# tidy up column name to match with other column names
colnames(dictionary_df)[2] <- "state_name"

# merge to get state names
x <- merge(cities_df, dictionary_df, by = "city_name")

# merge to get city names
y <- merge(states_df, dictionary_df, by = "state_name")

# merge by city and state
result <- merge(x, y, by = c("state_name", "city_name"))
result
#       state_name           city_name city_population city_area state_population
# 1 Greater London              London         8673713      1572              123
# 2  Tyne and Wear           Gateshead          120046        NA              456
# 3  Tyne and Wear Newcastle Upon Tyne          289835       114              456