vincentarelbundock / countrycode

R package: Convert country names and country codes. Assigns region descriptors.
https://vincentarelbundock.github.io/countrycode
GNU General Public License v3.0
342 stars 84 forks source link

Surprising behavior with countryname() and invalid destination codes #336

Closed NilsEnevoldsen closed 1 year ago

NilsEnevoldsen commented 1 year ago

Is it intended that countryname() returns the country name if the entity exists in the codelist but the destination code isn't valid for that entity? Compare:

> countrycode::countrycode("antarctica", origin = "country.name", destination = "cowc")
[1] NA
Warning message:
Some values were not matched unambiguously: antarctica

> countrycode::countryname("antarctica", destination = "cowc")
[1] "Antarctica"

> countrycode::countryname("xyz", destination = "cowc")
[1] NA
Warning message:
Some values were not matched unambiguously: xyz

I don't know what guarantees we make about countryname(), but this seems wrong. I would expect an NA, similar to countrycode().

vincentarelbundock commented 1 year ago

Good catch. I agree with you that the second command should produce NA.

vincentarelbundock commented 1 year ago

Thanks a lot for the report. Fixed by commit dbc170281bf6798da1d25a7847d9b69928601bef. The countryname function gets a new nomatch argument:

library(countrycode)
x <- c("canada", "antarctica")
countryname(x)
# [1] "Canada"     "Antarctica"
countryname(x, destination = "cowc", warn = FALSE)
# [1] "CAN" NA
countryname(x, destination = "cowc", warn = FALSE, nomatch = x)
# [1] "CAN"        "antarctica"