Why does my R mutate across with rowSums not work (Error: Problem with `mutate()` input `..2`. x 'x' must be numeric ℹ Input `..2` is `rowSums(.)`.)?

Why does my R mutate across with rowSums not work (Error: Problem with `mutate()` input `..2`. x 'x' must be numeric ℹ Input `..2` is `rowSums(.)`.)?

我正在尝试学习如何在 R 中使用 across() 函数,我想用它做一个简单的 rowSums()。但是,我不断收到此错误:

Error: Problem with mutate() input ..2. x 'x' must be numeric ℹ Input ..2 is rowSums(., na.rm = TRUE).

然而,我所有的相关列 都是 数字。任何帮助任何解释为什么我收到此错误将不胜感激!

这是一个可重现的例子:

library(dplyr)
test <- tibble(resource_name = c("Justin", "Corey", "Justin"),
       project = c("P1", "P2", "P3"),
       sep_2021 = c(1, 2, NA),
       oct_2021 = c(5, 2, 1))


test %>%
  select(resource_name, project, sep_2021, oct_2021) %>%
  mutate(total = across(contains("_20")), rowSums(., na.rm = TRUE))

这就是我要去的原因

answer <-  tibble(resource_name = c("Justin", "Corey", "Justin"),
                  project = c("P1", "P2", "P3"),
                  sep_2021 = c(1, 2, NA),
                  oct_2021 = c(5, 2, 1),
                  total = c(6, 4, 1))

注意:我的真实数据集有很多列,顺序是可变的。因此,我真的想使用代码的 contains("_20") 部分而不是索引。

我们可以用adorn_totals

library(dplyr)
library(janitor)
test %>%
     adorn_totals("col", name = "total")

-输出

  resource_name project sep_2021 oct_2021 total
        Justin      P1        1        5     6
         Corey      P2        2        2     4
        Justin      P3       NA        1     1

使用 rowSumsacross,语法为

test %>% 
   mutate(total = rowSums(across(contains("_20")), na.rm = TRUE))

-输出

# A tibble: 3 x 5
  resource_name project sep_2021 oct_2021 total
  <chr>         <chr>      <dbl>    <dbl> <dbl>
1 Justin        P1             1        5     6
2 Corey         P2             2        2     4
3 Justin        P3            NA        1     1

在 OP 的代码中,across 选择了列,但是 rowSums 是针对整个数据 (.) 而不是选择的数据

更新: 正如 akrun 所评论的(见评论),我们使用 c_across

test %>%
    rowwise() %>% 
    mutate(total = sum(c_across(contains("_20")), na.rm = TRUE))

这是计算行总和的另一个 dplyr 选项(使用 rowwisesum:

test %>%
    rowwise() %>% 
    mutate(total = sum(across(contains("_20")), na.rm = TRUE))
  resource_name project sep_2021 oct_2021 total
  <chr>         <chr>      <dbl>    <dbl> <dbl>
1 Justin        P1             1        5     6
2 Corey         P2             2        2     4
3 Justin        P3            NA        1     1