R中是否有一个功能可以使我可以创建一个新的列,其中包括(1)完整的国家名称和(2)各自的大陆?
在我的数据集中,我只有给出的国家代码。我设法获取了完整的国家名称,但是由于某种原因,我无法创建一个带有完整国家名称&的新专栏。因此,无法使用相应的大陆创建新的列。
这就是我到目前为止所做的:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在寻找吗
?
请注意,
countryCode()
无法明确识别所有字符串“ ISO3C”(国家代码)。请参阅警告以识别哪个顺便说一句,我认为在此处不需要使用
{dplyr}
。数据
Are you looking for
?
Note,
countrycode()
does not manage to identify all strings "iso3c" (country codes) unambiguously. See the warnings to identify whichBtw, I see no need to use
{dplyr}
here.Data
首先,从mycodes-vector创建一个tibble/dataframe,以便您可以在dplyr管道中喂食。在
dplyr :: mutate()
call中使用countrycode :: countryCode()
- 功能 - 连接新的变量固定的国家名称和各个非洲大陆:由
First, create a tibble/dataframe from the mycodes-vector such that you can feed it in dplyr pipe. Use the
countrycode::countrycode()
-function in adplyr::mutate()
-call to attach the new variable scontaining the countrynames and the respective continent:Created on 2022-05-26 by the reprex package (v0.3.0)