我有一个很大的数据框(旅行),其中包含许多单独的旅行历史记录。在travel $ Travel 1 Destination
列中,我只需要国家/地区数据,但是有很多条目输入了地区,城市或其他数据。
我尝试编写带有2个参数的函数(city_replace_with_country):
我希望我的函数在数据框中找到这些错误,将它们粘贴到另一个预先存在的列中(travel $ regions_visited),并用正确的方式将travel $`Travel 1 Destination'中的城市/地区名称替换为国家名称。
此代码可用于单个示例,例如。班加罗尔和印度
for (i in 1:nrow(travel) ) {
if(grepl("bangalore", travel$`Travel 1 Destination`[i], ignore.case = TRUE)){
travel$regions_visited[i] <- paste(travel$regions_visited[i], "Bangalore", sep = " ")
travel$`Travel 1 Destination`[i] <- gsub("bangalore", "india", travel$`Travel 1 Destination`[i], ignore.case = TRUE, perl = TRUE)
}}
理想情况下,我可以使用该功能对数据框进行许多修正
city_replace_with_country <- function(city_name, country_name) {
for (i in 1:nrow(travel)) {
if(grepl(city_name, travel$`Travel 1 Destination`[i], ignore.case = TRUE)){
travel$regions_visited[i] <- paste(travel$regions_visited[i], city_name, sep = " ")
travel$`Travel 1 Destination`[i] <- gsub(city_name, country_name, travel$`Travel 1 Destination`[i], ignore.case = TRUE, perl = TRUE)
}}}
当我尝试如下使用此功能时:
city_replace_with_country("bangalore", "india")
该函数的输出似乎未写入/存储到数据帧。 如果我尝试:
travel <- city_replace_with_country("bangalore", "india")
或
travel$`Travel 1 Destination` <- city_replace_with_country("bangalore", "india")
它返回NULL值。
任何有关如何使其正常工作的建议将不胜感激。非常感谢。
这是一些示例数据。例如,在这种情况下,我想将波哥大更改为哥伦比亚:
travel_example <- structure(list(X1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21), `Travel 1 Dates` = c("03/09/18 to 02/10/18",
NA, "15/08/18 - 24/08/18", "13/09/2018 to 19/09/2018", "15/07- 14/08",
"21/9/18-29/9/18", "10/09/18-3/10/18", "28/5/18-31/7/18", "1/9/18-16/9/18",
NA, NA, NA, "15/07/18-03/10/18", "15/09/18-30/09/18", "18/09/18-05/10/18",
"7/9/18-23/9/18", "14/8/18-11/9/18", "25/7/18-13/8/18", "24/9/18-30/9/18",
NA, "18/9/18-2/10/18"), `Travel 1 Destination` = c("Colombia",
"salvador, Bogata, Honduras", "China,Cambodia", "Lagos, Nigeria",
"Uganda", "Indonesia", "Kenya", "Dubai, Japan", "Sri Lanka",
"Jakarta,Indonesia", "South Africa, Mozambique, Ethiopia", NA,
"Ukraine, Slovakia, India", "Tanzania", "Ghana", "Sri Lanka",
"Angola", "Tajikistan , Afghanistan", "Morocco", NA, "Tanzania"
), regions_visited = c("character(0)", "character(0)", "character(0)",
"character(0)", "character(0)", "character(0)", "Nairobi", "character(0)",
"character(0)", "character(0)", "character(0)", "character(0)",
"character(0)", "character(0)", "character(0)", "character(0)",
"Luanda only", "c(\"south\", \"north\", \"very rural\")", "character(0)",
"character(0)", "character(0)")), class = c("spec_tbl_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -21L), spec = structure(list(
cols = list(X1 = structure(list(), class = c("collector_double",
"collector")), `Travel 1 Dates` = structure(list(), class = c("collector_character",
"collector")), `Travel 1 Destination` = structure(list(), class = c("collector_character",
"collector")), regions_visited = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
答案 0 :(得分:0)
在您的情况下,您可以使用regex
提取逗号后的最后一个单词,并获得所有国家/地区名称,如下所示:
travel_example$`Travel 1 Destination` <- sub('.*,\\s*', '', travel_example$`Travel 1 Destination`)
travel_example$`Travel 1 Destination`
[1] "Colombia" "Honduras" "Cambodia" "Nigeria" "Uganda"
[6] "Indonesia" "Kenya" "Japan" "Sri Lanka" "Indonesia"
[11] "Ethiopia" NA "India" "Tanzania" "Ghana"
[16] "Sri Lanka" "Angola" "Afghanistan" "Morocco" NA
[21] "Tanzania"