-2

有人可以帮助我在 R 中实现一个想法吗?

我想要实现的是,当 R 获得一个包含公司列表及其地址的输入文件时,它将检查每个公司的邮政编码是否适合城市。我有来自某个国家/地区的所有城市和邮政编码的列表。如何将列表实现为 if 语句?

以前有人编程过类似的东西吗?

感谢您的帮助!桑德拉

4

1 回答 1

0

只是一个可以做什么的简单例子。但是,对您的城市使用模糊匹配可能会更好。

# City codes (all city codes can be found at https://www.allareacodes.com/)
my_city_codes <- data.frame(code = c(201:206), 
                            cities = c("Jersey City, NJ", "District of Columbia", "Bridgeport, CT", "Manitoba", "Birmingham, AL", "Seattle, WA"),
                            stringsAsFactors = FALSE)

# Function for checking if city/city-code matches those in the registries
adress_checker <- function(adress, citycodes) {
  # Finding real city
  real_city <- my_city_codes$cities[which(adress$code == my_city_codes$code)]

  # Checking if cities are the same
  if(real_city == adress$city) {
    return("Correct city")
  } else {
    return("Incorrect city")
  }
}

# Adresses to check
right_city <- data.frame(code = 205, city = c("Birmingham, AL"), stringsAsFactors = FALSE)
wrong_city <- data.frame(code = 205, city = c("Las Vegas"), stringsAsFactors = FALSE)

# Testing function
adress_checker(right_city, my_city_codes)
[1] "Correct city"
adress_checker(wrong_city, my_city_codes)
[1] "Incorrect city"
于 2018-12-12T10:04:02.073 回答