dplyr::coalesce在列名缺失时抛出错误。

huangapple go评论60阅读模式
英文:

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 &lt;- c(&quot;Email Address&quot;, &quot;Email&quot;, &quot;E-mail Address&quot;, &quot;E-MAIL&quot;, &quot;email&quot;)
# Sample data
set.seed(12442)
dat &lt;- data.frame(matrix(sample(c(1:10, NA), 25, replace = TRUE), 5, 5, 
                  dimnames = list(NULL, cols)), check.names = FALSE)
dat %&gt;%
  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 &lt;- NULL

dat %&gt;%
   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 

huangapple
  • 本文由 发表于 2023年4月10日 18:39:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定