R 将NA值替换为基于其他列中唯一值的字符
情况:R 将NA值替换为基于其他列中唯一值的字符,r,dataframe,dplyr,R,Dataframe,Dplyr,情况: structure(list(country = c("EL", "EL", "EL", "EL", "EL", "UK", "UK", "UK", "UK", "UK"), country_name = c(NA_character_, NA_character_, NA_ch
structure(list(country = c("EL", "EL", "EL", "EL", "EL", "UK",
"UK", "UK", "UK", "UK"), country_name = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), type = c("Conventional thermal",
"Conventional thermal", "Conventional thermal", "Nuclear", "Nuclear",
"Conventional thermal", "Conventional thermal", "Conventional thermal",
"Nuclear", "Nuclear")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
我有一个很大的数据集,其中有一列是国家代码(country
),旁边有一列是国家名称(country\u name
)。我需要帮助将country
列中两个观察值的NA值替换为相应country\u name
列中的NA:
国家
列在国家名称
列中有正确的信息
可复制数据:
structure(list(country = c("EL", "EL", "EL", "EL", "EL", "UK",
"UK", "UK", "UK", "UK"), country_name = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), type = c("Conventional thermal",
"Conventional thermal", "Conventional thermal", "Nuclear", "Nuclear",
"Conventional thermal", "Conventional thermal", "Conventional thermal",
"Nuclear", "Nuclear")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
对于大型数据集,我建议使用映射文件:
country_map <- tibble(country = c("EL", "UK"),
country_name2 = c("Greece", "United Kingdom"))
对于大型数据集,我建议使用映射文件:
country_map <- tibble(country = c("EL", "UK"),
country_name2 = c("Greece", "United Kingdom"))
您尝试了什么,在哪里发现了问题?此外,文章还带有
dplyr
和data.table
标记。你喜欢哪一种套餐?BaseR解决方案有效吗?您尝试了什么,在哪里发现了问题?此外,文章还带有dplyr
和data.table
标记。你喜欢哪一种套餐?BaseR解决方案有效吗?