0

遵循以下文档?across

across(.cols = everything(), .fns = NULL, ..., .names = NULL)

我用作everything()LHS 的参考。

然而,似乎也everything()没有从管道接收 LHS 对象。.

我错过了什么?

重复:

library(dplyr)
# this works as expected
iris %>%
  select(Sepal.Length:Petal.Width) %>%
  mutate_all(as.character) %>% glimpse

Rows: 150
Columns: 2
$ Sepal.Length <chr> "5.1", "4.9", "4.7", "4.6", "5", "5.4", "4.6", "5…
$ Sepal.Width  <chr> "3.5", "3", "3.2", "3.1", "3.6", "3.9", "3.4", "3…

# this doesn't work
iris %>%
  select(Sepal.Length:Sepal.Width) %>%
  mutate(across(.cols = everything(), .fns = as.character)) %>% glimpse

Rows: 150
Columns: 2
$ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9,…
$ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1,…
4

1 回答 1

0

经过繁琐的反复试验,我找到了解决方案。我发布它是因为我认为它不是微不足道的,特别是对于 dplyr 的初学者。

出现此问题的原因是mutate来自 dplyr 库的调用可以被覆盖,很可能被plyr库覆盖。棘手的方面是后一个库作为其他库的依赖项加载。在这种情况下,您需要在加载库后仔​​细阅读警告消息。

就我而言,plyr由以下人员加载ggbiplot

library(ggbiplot)
Loading required package: plyr
-----------------------------------------------------------------------
You have loaded plyr after dplyr - this is likely to cause problems.
If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
library(plyr); library(dplyr)
-----------------------------------------------------------------------

重要的是,警告消息明确命名了那些被覆盖的 dplyr 函数:

Attaching package: ‘plyr’

The following objects are masked from ‘package:dplyr’:

    arrange, count, desc, failwith, id, mutate, rename,
    summarise, summarize

在这种情况下,修复很简单:

library(plyr)
library(dplyr)
library(ggiplot)

在我看来,最安全的方法是确保从 dplyr 调用每个 dplyr 函数:

iris %>%
  dplyr::select(Sepal.Length:Sepal.Width) %>%
  dplyr::mutate(across(.cols = everything(), .fns = as.character)) %>% glimpse
于 2020-09-16T13:56:14.473 回答