如何将国家代码转换为国家名称
How to convert country codes into country names
这是我的可重现代码:
library(tidyverse)
library(eurostat)
library(countrycode)
#Query eurostat data set
search <- search_eurostat("road", type = "table")
#Accessing "People killed in road accidents" data set.
df <- get_eurostat("tsdtr420")
#Create a data frame filtered on most recent data (2015)
accidents2015 <- df %>%
filter(time == "2015-01-01")
codes <- select(accidents2015, geo)
countrycode(sourcevar = codes, "iso2c", "country_name")
当最后一行运行时,我收到此错误消息:
the condition has length > 1 and only the first element will be usedError in countrycode(sourcevar = codes, "iso2c", "country_name") :
sourcevar must be a character or numeric vector.
首先codes
是一个数据框,你需要传递一个向量,即使用数据框的列。此外,将 class 从一个因子更改为一个字符。最后是 "country.name"
而不是 "country_name"
:
codes$geo = as.character(codes$geo)
countrycode(sourcevar = codes$geo, "iso2c", "country.name")
[1] "Austria" "Belgium" "Switzerland" "Cyprus" "Czech Republic" "Germany" "Denmark"
[8] "Estonia" NA "Spain" "Finland" "France" "Croatia" "Hungary"
[15] "Iceland" "Italy" "Lithuania" "Luxembourg" "Latvia" "Malta" "Netherlands"
[22] "Norway" "Poland" "Portugal" "Romania" "Slovenia" NA
传递一个向量,而不是数据 frame/tibble 作为 sourcevar
使用country.name
,而不是country_name
作为目的地代码
在您的具体情况下,您最好使用 eurostat
作为原始代码
例如...
library(tidyverse)
library(eurostat)
library(countrycode)
accidents2015 <-
get_eurostat("tsdtr420") %>%
filter(time == "2015-01-01")
countrycode(sourcevar = accidents2015$geo, "eurostat", "country.name")
这是我的可重现代码:
library(tidyverse)
library(eurostat)
library(countrycode)
#Query eurostat data set
search <- search_eurostat("road", type = "table")
#Accessing "People killed in road accidents" data set.
df <- get_eurostat("tsdtr420")
#Create a data frame filtered on most recent data (2015)
accidents2015 <- df %>%
filter(time == "2015-01-01")
codes <- select(accidents2015, geo)
countrycode(sourcevar = codes, "iso2c", "country_name")
当最后一行运行时,我收到此错误消息:
the condition has length > 1 and only the first element will be usedError in countrycode(sourcevar = codes, "iso2c", "country_name") : sourcevar must be a character or numeric vector.
首先codes
是一个数据框,你需要传递一个向量,即使用数据框的列。此外,将 class 从一个因子更改为一个字符。最后是 "country.name"
而不是 "country_name"
:
codes$geo = as.character(codes$geo)
countrycode(sourcevar = codes$geo, "iso2c", "country.name")
[1] "Austria" "Belgium" "Switzerland" "Cyprus" "Czech Republic" "Germany" "Denmark"
[8] "Estonia" NA "Spain" "Finland" "France" "Croatia" "Hungary"
[15] "Iceland" "Italy" "Lithuania" "Luxembourg" "Latvia" "Malta" "Netherlands"
[22] "Norway" "Poland" "Portugal" "Romania" "Slovenia" NA
传递一个向量,而不是数据 frame/tibble 作为 sourcevar
使用
country.name
,而不是country_name
作为目的地代码在您的具体情况下,您最好使用
eurostat
作为原始代码
例如...
library(tidyverse)
library(eurostat)
library(countrycode)
accidents2015 <-
get_eurostat("tsdtr420") %>%
filter(time == "2015-01-01")
countrycode(sourcevar = accidents2015$geo, "eurostat", "country.name")