Closed jolyphil closed 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')
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 👍🏻
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.
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"
I think @cjyetman meant to link to commit 75e3263b8e53372a84e0a5d6bd3da2f408a44807. (The other link, at least for me, goes to a broken issues link.)
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.
🤷🏻
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.
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...
... will return this warning message:
(the condition has length > 1 and only the first element will be used)
Any idea what is causing the problem?