0

Following the documentation of ?across:

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

I used everything() as reference to the LHS.

Yet neither everything() nor . seems to receive the LHS object from the pipe.

What am I missing?

Repex:

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,…
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Agile Bean
  • 6,437
  • 1
  • 45
  • 53

1 Answers1

0

After tedious trial & error, I found the solution. I am posting it because I think it is not trivial, especially for beginners of dplyr.

The reason this problem occurs is that the mutate from the dplyr library call can be overridden, most likely by the plyr library. The tricky aspect is that the latter library is loaded as dependency of other libraries. In that case, you need to read closely the warnings messages after loading the library.

In my case, plyr was loaded by 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)
-----------------------------------------------------------------------

Importantly, the warning messages explicitly names those dplyr functions which are overridden:

Attaching package: ‘plyr’

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

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

In this case, the fix is easy:

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

In my opinion the safest way is to make sure every dplyr function is called from dplyr:

iris %>%
  dplyr::select(Sepal.Length:Sepal.Width) %>%
  dplyr::mutate(across(.cols = everything(), .fns = as.character)) %>% glimpse
Agile Bean
  • 6,437
  • 1
  • 45
  • 53