英文:
dplyr::coalesce throws error when column name is missing
问题
zed <- zed %>%
dplyr::mutate(Email = coalesce(`电子邮件地址`, Email, `电子邮件地址`, `E-MAIL`, email, `E-Mail`, `E-mail`, `电子邮件地址`, `EMAIL ADDRESS`, EMAIL))
英文:
zed <- zed %>%
dplyr::mutate(Email = coalesce(`Email Address`, Email, `E-mail Address`, `E-MAIL`, email, `E-Mail`, `E-mail`, `E-Mail Address`, `EMAIL ADDRESS`, EMAIL))
Error in `mutate_cols()`:
! Problem with `mutate()` column `Email`.
ℹ `Email = coalesce(...)`.
✖ object 'E-mail Address' not found
Caused by error in `list2()`:
! object 'E-mail Address' not found
Run `rlang::last_error()` to see where the error occurred.
How can we handle using coalesce
to consolidate all of these columns into one column, while also handling the potential for one or more of these columns to be missing. Simply removing E-mail Address
is not great because in other scenarios used by our function, E-mail Address
will in fact exist.
答案1
得分: 3
以下是翻译好的代码部分:
library(dplyr)
cols <- c("Email Address", "Email", "E-mail Address", "E-MAIL", "email")
# 示例数据
set.seed(12442)
dat <- data.frame(matrix(sample(c(1:10, NA), 25, replace = TRUE), 5, 5,
dimnames = list(NULL, cols)), check.names = FALSE)
dat %>%
mutate(Email_new = do.call(coalesce, pick(any_of(cols))))
# Email Address Email E-mail Address E-MAIL email Email_new
#1 7 7 8 4 10 7
#2 7 7 4 1 3 7
#3 2 7 NA 3 9 2
#4 NA 4 6 9 9 4
#5 6 6 1 10 8 6
再次删除一列,然后使用相同的代码尝试:
dat$email <- NULL
dat %>%
mutate(Email_new = do.call(coalesce, pick(any_of(cols))))
# Email Address Email E-mail Address E-MAIL Email_new
#1 7 7 8 4 7
#2 7 7 4 1 7
#3 2 7 NA 3 2
#4 NA 4 6 9 4
#5 6 6 1 10 6
英文:
You can do this with the help of do.call
-
library(dplyr)
cols <- c("Email Address", "Email", "E-mail Address", "E-MAIL", "email")
# Sample data
set.seed(12442)
dat <- data.frame(matrix(sample(c(1:10, NA), 25, replace = TRUE), 5, 5,
dimnames = list(NULL, cols)), check.names = FALSE)
dat %>%
mutate(Email_new = do.call(coalesce, pick(any_of(cols))))
# Email Address Email E-mail Address E-MAIL email Email_new
#1 7 7 8 4 10 7
#2 7 7 4 1 3 7
#3 2 7 NA 3 9 2
#4 NA 4 6 9 9 4
#5 6 6 1 10 8 6
Now let's delete one of the column and try again with the same code.
dat$email <- NULL
dat %>%
mutate(Email_new = do.call(coalesce, pick(any_of(cols))))
# Email Address Email E-mail Address E-MAIL Email_new
#1 7 7 8 4 7
#2 7 7 4 1 7
#3 2 7 NA 3 2
#4 NA 4 6 9 4
#5 6 6 1 10 6
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论