R中是否有一个功能可以使我可以创建一个新的列,其中包括(1)完整的国家名称和(2)各自的大陆?

发布于 2025-02-01 10:41:17 字数 2227 浏览 2 评论 0原文

在我的数据集中,我只有给出的国家代码。我设法获取了完整的国家名称,但是由于某种原因,我无法创建一个带有完整国家名称&的新专栏。因此,无法使用相应的大陆创建新的列。

这就是我到目前为止所做的:

mycodes <- c("PRT", "FRA", "JPN", "IRL", "ESP", "BEL", "AUT", "DEU", "ITA", "CHN", "RUS", "POL", "USA", "CRI", "CHE", "ROU", "GBR",
             "BRA", "FIN", "NLD", "CAN", "ZAF", "AUS", "AGO", "BGR", "SWE", "CYP", "ARG", "ARM", "CHL", "MOZ", "KOR", "TUN", "DNK", "GRC",
             "NOR", "ISR", "MYS", "EGy", "JOR", "LUX", "TUR", "IRN", "LBY", "PAN", "COL", "VEN", "DZA", "GNB", "MAR", "CZE", "SVN", "IND",
             "HUN", "NZL", "PER", "LTU", "TWN", "SRB", "EST", "KAZ", "KWT", "IDN", "UKR", "MEX", "SVK", "SAU", "ARE", "BGD", "THA", "TZA", 
             "LVA", "PHL", "BIH", "BHR", "NAM", "BOL", "HRV", "SGP", "CMR", "MLT", "URY", "PAK", "JAM", "ECU", "SYC", "QAT", "PRY", "BRB",
             "OMN", "TMP", "ABW", "LBN", "SLV", "DMA", "CUB", "VNM", "GEO", "IRQ", "PYF", "UGA", "LIE", "SEN", "BLR", "ISL", "DOM",
             "GUY", "LCA", "CPV", "ATA", "GAB", "NGA", "RWA", "CIV", "ALB", "MKD", "MNE", "GTM", "GHA", "MDV", "MCO", "MUS", "TGO", "LKA",
             "AZE", "SUR", "KEN", "MRT", "HKG", "SYR", "CAF", "NCL", "UZB", "KIR", "SDN", "PRI", "ATF", "KNA", "TJK", "SLE", "LAO", "COM",
             "ETH", "FRO", "AND", "BEN", "ZWE", "ASM", "MLI", "BWA", "AIA", "COD", "SPM", "JEY", "MDG", "NIC", "SWZ", "CYM", "SOM", "ATG",
             "KGZ", "FLK", "GIB", "SMR", "TKM", "HTI", "UMI", "MMR", "WSM", "VIR", "ERI", "WLF", "GUF", "MWI", "PCN", "TCD")
countrycode(mycodes, "iso3c", "country.name")
x <- c(countrycode)
mutate(NEWHotelCustomers, Nationality_Customers = countrycode(mycodes, "iso3c", "country.name"))
View(NEWHotelCustomers)

那是我收到的错误消息:

Error in `mutate()`:
! Problem while computing `Nationality_Customers = countrycode(mycodes, "iso3c", "country.name")`.
x `Nationality_Customers` must be size 58248 or 1, not 176.
Run `rlang::last_error()` to see where the error occurred.
Warning message:
Problem while computing `Nationality_Customers = countrycode(mycodes, "iso3c", "country.name")`.
i Some values were not matched unambiguously: TMP 

预先感谢!

In my dataset I have only the country codes given. I managed to get the full country names, however for some reason I cannot create a new column with the full country names & consequently cannot create a new column with the corresponding continent.

That is what I have done so far:

mycodes <- c("PRT", "FRA", "JPN", "IRL", "ESP", "BEL", "AUT", "DEU", "ITA", "CHN", "RUS", "POL", "USA", "CRI", "CHE", "ROU", "GBR",
             "BRA", "FIN", "NLD", "CAN", "ZAF", "AUS", "AGO", "BGR", "SWE", "CYP", "ARG", "ARM", "CHL", "MOZ", "KOR", "TUN", "DNK", "GRC",
             "NOR", "ISR", "MYS", "EGy", "JOR", "LUX", "TUR", "IRN", "LBY", "PAN", "COL", "VEN", "DZA", "GNB", "MAR", "CZE", "SVN", "IND",
             "HUN", "NZL", "PER", "LTU", "TWN", "SRB", "EST", "KAZ", "KWT", "IDN", "UKR", "MEX", "SVK", "SAU", "ARE", "BGD", "THA", "TZA", 
             "LVA", "PHL", "BIH", "BHR", "NAM", "BOL", "HRV", "SGP", "CMR", "MLT", "URY", "PAK", "JAM", "ECU", "SYC", "QAT", "PRY", "BRB",
             "OMN", "TMP", "ABW", "LBN", "SLV", "DMA", "CUB", "VNM", "GEO", "IRQ", "PYF", "UGA", "LIE", "SEN", "BLR", "ISL", "DOM",
             "GUY", "LCA", "CPV", "ATA", "GAB", "NGA", "RWA", "CIV", "ALB", "MKD", "MNE", "GTM", "GHA", "MDV", "MCO", "MUS", "TGO", "LKA",
             "AZE", "SUR", "KEN", "MRT", "HKG", "SYR", "CAF", "NCL", "UZB", "KIR", "SDN", "PRI", "ATF", "KNA", "TJK", "SLE", "LAO", "COM",
             "ETH", "FRO", "AND", "BEN", "ZWE", "ASM", "MLI", "BWA", "AIA", "COD", "SPM", "JEY", "MDG", "NIC", "SWZ", "CYM", "SOM", "ATG",
             "KGZ", "FLK", "GIB", "SMR", "TKM", "HTI", "UMI", "MMR", "WSM", "VIR", "ERI", "WLF", "GUF", "MWI", "PCN", "TCD")
countrycode(mycodes, "iso3c", "country.name")
x <- c(countrycode)
mutate(NEWHotelCustomers, Nationality_Customers = countrycode(mycodes, "iso3c", "country.name"))
View(NEWHotelCustomers)

That is the error message I received:

Error in `mutate()`:
! Problem while computing `Nationality_Customers = countrycode(mycodes, "iso3c", "country.name")`.
x `Nationality_Customers` must be size 58248 or 1, not 176.
Run `rlang::last_error()` to see where the error occurred.
Warning message:
Problem while computing `Nationality_Customers = countrycode(mycodes, "iso3c", "country.name")`.
i Some values were not matched unambiguously: TMP 

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油饼 2025-02-08 10:41:17

你在寻找吗

library(countrycode)

df <- data.frame(country_code = mycodes,
                country_name = countrycode(sourcevar = mycodes, origin = "iso3c", 
                                    destination = "country.name"),
                continent = countrycode(sourcevar = mycodes, origin = "iso3c", 
                                        destination = "continent"))

head(df)

请注意,countryCode()无法明确识别所有字符串“ ISO3C”(国家代码)。请参阅警告以识别哪个

#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: TMP
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: ATA, ATF, TMP, UMI

顺便说一句,我认为在此处不需要使用{dplyr}

数据


mycodes <- c("PRT", "FRA", "JPN", "IRL", "ESP", "BEL", "AUT", "DEU", "ITA", "CHN", "RUS", "POL", "USA", "CRI", "CHE", "ROU", "GBR",
             "BRA", "FIN", "NLD", "CAN", "ZAF", "AUS", "AGO", "BGR", "SWE", "CYP", "ARG", "ARM", "CHL", "MOZ", "KOR", "TUN", "DNK", "GRC",
             "NOR", "ISR", "MYS", "EGy", "JOR", "LUX", "TUR", "IRN", "LBY", "PAN", "COL", "VEN", "DZA", "GNB", "MAR", "CZE", "SVN", "IND",
             "HUN", "NZL", "PER", "LTU", "TWN", "SRB", "EST", "KAZ", "KWT", "IDN", "UKR", "MEX", "SVK", "SAU", "ARE", "BGD", "THA", "TZA", 
             "LVA", "PHL", "BIH", "BHR", "NAM", "BOL", "HRV", "SGP", "CMR", "MLT", "URY", "PAK", "JAM", "ECU", "SYC", "QAT", "PRY", "BRB",
             "OMN", "TMP", "ABW", "LBN", "SLV", "DMA", "CUB", "VNM", "GEO", "IRQ", "PYF", "UGA", "LIE", "SEN", "BLR", "ISL", "DOM",
             "GUY", "LCA", "CPV", "ATA", "GAB", "NGA", "RWA", "CIV", "ALB", "MKD", "MNE", "GTM", "GHA", "MDV", "MCO", "MUS", "TGO", "LKA",
             "AZE", "SUR", "KEN", "MRT", "HKG", "SYR", "CAF", "NCL", "UZB", "KIR", "SDN", "PRI", "ATF", "KNA", "TJK", "SLE", "LAO", "COM",
             "ETH", "FRO", "AND", "BEN", "ZWE", "ASM", "MLI", "BWA", "AIA", "COD", "SPM", "JEY", "MDG", "NIC", "SWZ", "CYM", "SOM", "ATG",
             "KGZ", "FLK", "GIB", "SMR", "TKM", "HTI", "UMI", "MMR", "WSM", "VIR", "ERI", "WLF", "GUF", "MWI", "PCN", "TCD")

Are you looking for

library(countrycode)

df <- data.frame(country_code = mycodes,
                country_name = countrycode(sourcevar = mycodes, origin = "iso3c", 
                                    destination = "country.name"),
                continent = countrycode(sourcevar = mycodes, origin = "iso3c", 
                                        destination = "continent"))

head(df)

?

Note, countrycode() does not manage to identify all strings "iso3c" (country codes) unambiguously. See the warnings to identify which

#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: TMP
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: ATA, ATF, TMP, UMI

Btw, I see no need to use {dplyr} here.

Data


mycodes <- c("PRT", "FRA", "JPN", "IRL", "ESP", "BEL", "AUT", "DEU", "ITA", "CHN", "RUS", "POL", "USA", "CRI", "CHE", "ROU", "GBR",
             "BRA", "FIN", "NLD", "CAN", "ZAF", "AUS", "AGO", "BGR", "SWE", "CYP", "ARG", "ARM", "CHL", "MOZ", "KOR", "TUN", "DNK", "GRC",
             "NOR", "ISR", "MYS", "EGy", "JOR", "LUX", "TUR", "IRN", "LBY", "PAN", "COL", "VEN", "DZA", "GNB", "MAR", "CZE", "SVN", "IND",
             "HUN", "NZL", "PER", "LTU", "TWN", "SRB", "EST", "KAZ", "KWT", "IDN", "UKR", "MEX", "SVK", "SAU", "ARE", "BGD", "THA", "TZA", 
             "LVA", "PHL", "BIH", "BHR", "NAM", "BOL", "HRV", "SGP", "CMR", "MLT", "URY", "PAK", "JAM", "ECU", "SYC", "QAT", "PRY", "BRB",
             "OMN", "TMP", "ABW", "LBN", "SLV", "DMA", "CUB", "VNM", "GEO", "IRQ", "PYF", "UGA", "LIE", "SEN", "BLR", "ISL", "DOM",
             "GUY", "LCA", "CPV", "ATA", "GAB", "NGA", "RWA", "CIV", "ALB", "MKD", "MNE", "GTM", "GHA", "MDV", "MCO", "MUS", "TGO", "LKA",
             "AZE", "SUR", "KEN", "MRT", "HKG", "SYR", "CAF", "NCL", "UZB", "KIR", "SDN", "PRI", "ATF", "KNA", "TJK", "SLE", "LAO", "COM",
             "ETH", "FRO", "AND", "BEN", "ZWE", "ASM", "MLI", "BWA", "AIA", "COD", "SPM", "JEY", "MDG", "NIC", "SWZ", "CYM", "SOM", "ATG",
             "KGZ", "FLK", "GIB", "SMR", "TKM", "HTI", "UMI", "MMR", "WSM", "VIR", "ERI", "WLF", "GUF", "MWI", "PCN", "TCD")
一梦浮鱼 2025-02-08 10:41:17

首先,从mycodes-vector创建一个tibble/dataframe,以便您可以在dplyr管道中喂食。在dplyr :: mutate() call中使用countrycode :: countryCode() - 功能 - 连接新的变量固定的国家名称和各个非洲大陆:

library(countrycode)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
mycodes <- c("PRT", "FRA", "JPN", "IRL", "ESP", "BEL", "AUT", "DEU", "ITA", "CHN", "RUS", "POL", "USA", "CRI", "CHE", 
             "ROU", "GBR", "BRA", "FIN", "NLD", "CAN", "ZAF", "AUS", "AGO", "BGR", "SWE", "CYP", "ARG", "ARM", "CHL", 
             "MOZ", "KOR", "TUN", "DNK", "GRC", "NOR", "ISR", "MYS", "EGy", "JOR", "LUX", "TUR", "IRN", "LBY", "PAN", 
             "COL", "VEN", "DZA", "GNB", "MAR", "CZE", "SVN", "IND", "HUN", "NZL", "PER", "LTU", "TWN", "SRB", "EST", 
             "KAZ", "KWT", "IDN", "UKR", "MEX", "SVK", "SAU", "ARE", "BGD", "THA", "TZA", "LVA", "PHL", "BIH", "BHR", 
             "NAM", "BOL", "HRV", "SGP", "CMR", "MLT", "URY", "PAK", "JAM", "ECU", "SYC", "QAT", "PRY", "BRB", "OMN", 
             "TMP", "ABW", "LBN", "SLV", "DMA", "CUB", "VNM", "GEO", "IRQ", "PYF", "UGA", "LIE", "SEN", "BLR", "ISL", 
             "DOM", "GUY", "LCA", "CPV", "ATA", "GAB", "NGA", "RWA", "CIV", "ALB", "MKD", "MNE", "GTM", "GHA", "MDV", 
             "MCO", "MUS", "TGO", "LKA", "AZE", "SUR", "KEN", "MRT", "HKG", "SYR", "CAF", "NCL", "UZB", "KIR", "SDN", 
             "PRI", "ATF", "KNA", "TJK", "SLE", "LAO", "COM", "ETH", "FRO", "AND", "BEN", "ZWE", "ASM", "MLI", "BWA", 
             "AIA", "COD", "SPM", "JEY", "MDG", "NIC", "SWZ", "CYM", "SOM", "ATG", "KGZ", "FLK", "GIB", "SMR", "TKM", 
             "HTI", "UMI", "MMR", "WSM", "VIR", "ERI", "WLF", "GUF", "MWI", "PCN", "TCD") 
df <- tibble(codes = mycodes)

df <- df %>%
  mutate(countryname = countrycode(mycodes, "iso3c", "country.name"),
         continent = countrycode(mycodes, "iso3c", "continent") )
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: TMP
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: ATA, ATF, TMP, UMI

head(df)
#> # A tibble: 6 x 3
#>   codes countryname continent
#>   <chr> <chr>       <chr>    
#> 1 PRT   Portugal    Europe   
#> 2 FRA   France      Europe   
#> 3 JPN   Japan       Asia     
#> 4 IRL   Ireland     Europe   
#> 5 ESP   Spain       Europe   
#> 6 BEL   Belgium     Europe

First, create a tibble/dataframe from the mycodes-vector such that you can feed it in dplyr pipe. Use the countrycode::countrycode()-function in a dplyr::mutate()-call to attach the new variable scontaining the countrynames and the respective continent:

library(countrycode)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
mycodes <- c("PRT", "FRA", "JPN", "IRL", "ESP", "BEL", "AUT", "DEU", "ITA", "CHN", "RUS", "POL", "USA", "CRI", "CHE", 
             "ROU", "GBR", "BRA", "FIN", "NLD", "CAN", "ZAF", "AUS", "AGO", "BGR", "SWE", "CYP", "ARG", "ARM", "CHL", 
             "MOZ", "KOR", "TUN", "DNK", "GRC", "NOR", "ISR", "MYS", "EGy", "JOR", "LUX", "TUR", "IRN", "LBY", "PAN", 
             "COL", "VEN", "DZA", "GNB", "MAR", "CZE", "SVN", "IND", "HUN", "NZL", "PER", "LTU", "TWN", "SRB", "EST", 
             "KAZ", "KWT", "IDN", "UKR", "MEX", "SVK", "SAU", "ARE", "BGD", "THA", "TZA", "LVA", "PHL", "BIH", "BHR", 
             "NAM", "BOL", "HRV", "SGP", "CMR", "MLT", "URY", "PAK", "JAM", "ECU", "SYC", "QAT", "PRY", "BRB", "OMN", 
             "TMP", "ABW", "LBN", "SLV", "DMA", "CUB", "VNM", "GEO", "IRQ", "PYF", "UGA", "LIE", "SEN", "BLR", "ISL", 
             "DOM", "GUY", "LCA", "CPV", "ATA", "GAB", "NGA", "RWA", "CIV", "ALB", "MKD", "MNE", "GTM", "GHA", "MDV", 
             "MCO", "MUS", "TGO", "LKA", "AZE", "SUR", "KEN", "MRT", "HKG", "SYR", "CAF", "NCL", "UZB", "KIR", "SDN", 
             "PRI", "ATF", "KNA", "TJK", "SLE", "LAO", "COM", "ETH", "FRO", "AND", "BEN", "ZWE", "ASM", "MLI", "BWA", 
             "AIA", "COD", "SPM", "JEY", "MDG", "NIC", "SWZ", "CYM", "SOM", "ATG", "KGZ", "FLK", "GIB", "SMR", "TKM", 
             "HTI", "UMI", "MMR", "WSM", "VIR", "ERI", "WLF", "GUF", "MWI", "PCN", "TCD") 
df <- tibble(codes = mycodes)

df <- df %>%
  mutate(countryname = countrycode(mycodes, "iso3c", "country.name"),
         continent = countrycode(mycodes, "iso3c", "continent") )
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: TMP
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: ATA, ATF, TMP, UMI

head(df)
#> # A tibble: 6 x 3
#>   codes countryname continent
#>   <chr> <chr>       <chr>    
#> 1 PRT   Portugal    Europe   
#> 2 FRA   France      Europe   
#> 3 JPN   Japan       Asia     
#> 4 IRL   Ireland     Europe   
#> 5 ESP   Spain       Europe   
#> 6 BEL   Belgium     Europe

Created on 2022-05-26 by the reprex package (v0.3.0)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文