vincentarelbundock / countrycode

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

Warning message, ESS data #260

Closed jolyphil closed 3 years ago

jolyphil commented 3 years ago

Hi, thanks for this great package!

I am trying to convert country codes from the European Social Survey, but I am getting a warning message.

Running this code...

library(countrycode)
library(essurvey)
ess9 <- import_rounds(9, ess_email = "ess@byom.de", format = "spss")
countrycode(ess9$cntry, 'iso2c', 'iso3c')

... will return this warning message:

In if ((class(origin_vector) == "character") & !grepl("country",  :
  la condition a une longueur > 1 et seul le premier élément est utilisé

(the condition has length > 1 and only the first element will be used)

Any idea what is causing the problem?

vincentarelbundock commented 3 years ago

Your data is in a weird format, and not in a standard "base R" class such as character or numeric. Just convert it:

countrycode(as.character(ess9$cntry), 'iso2c', 'iso3c')
cjyetman commented 3 years ago

more specifically, ess9$cntry is a haven_labelled vector, made by the package haven

class(ess9$cntry)
[1] "haven_labelled" "vctrs_vctr"     "character"   

...and @vincentarelbundock has already made an improvement with commit 75e3263b8e53372a84e0a5d6bd3da2f408a44807 that will work around that in a future version 👍🏻

jolyphil commented 3 years ago

Thanks a lot for the quick response! I think more and more people are using haven_labelled to move data from SPSS/Stata/SAS to R. Finding a workaround sounds like a good idea.

cjyetman commented 3 years ago

In the meantime (before @vincentarelbundock's improvement lands in a release), you may want/need to use haven::as_factor to convert your vector rather than as.character, for example...

library(haven)

path <- system.file("examples", "iris.sav", package = "haven")
data <- read_sav(path)[c(1:5, 100:105), ]

data$Species
#> <labelled<double>[11]>
#>  [1] 1 1 1 1 1 2 3 3 3 3 3
#> 
#> Labels:
#>  value      label
#>      1     setosa
#>      2 versicolor
#>      3  virginica
class(data$Species)
#> [1] "haven_labelled" "vctrs_vctr"     "double"

as.character(data$Species)
#>  [1] "1" "1" "1" "1" "1" "2" "3" "3" "3" "3" "3"
class(as.character(data$Species))
#> [1] "character"

as_factor(data$Species)
#>  [1] setosa     setosa     setosa     setosa     setosa     versicolor
#>  [7] virginica  virginica  virginica  virginica  virginica 
#> Levels: setosa versicolor virginica
class(as_factor(data$Species))
#> [1] "factor"
NilsEnevoldsen commented 3 years ago

I think @cjyetman meant to link to commit 75e3263b8e53372a84e0a5d6bd3da2f408a44807. (The other link, at least for me, goes to a broken issues link.)

cjyetman commented 3 years ago

Yes, you're correct @NilsEnevoldsen. Thanks for catching that. I've edited my comment above. Apparently using the full commit hash auto links to the right place if it's on its own, but not if you use it inside of the [link text](url) markdown syntax for links. 🤷🏻

jolyphil commented 3 years ago

Thanks a lot for your help! The solution proposed by @vincentarelbundock was sufficient to solve my problem. I will keep in mind the option to use haven::as_factor in the future.