across() error - must be a vector, not a formula object

Dear all,

Can someone explain to me why first code chunk does not work but the second one works? Thank you.

library(tidyverse)

rownames_to_column(mtcars, var = "car") %>%
  mutate(across("car"), ~str_remove_all(., "\\d"))

#> Error: Problem with `mutate()` input `..2`.
#> i `..2 = ~str_remove_all(., "\\d")`.
#> x `..2` must be a vector, not a `formula` object.

Created on 2022-01-14 by the reprex package (v2.0.1)

mtcars %>%
   mutate(across(everything(), ~ifelse(.=="0", NA, as.character(.))))

Created on 2022-01-14 by the reprex package (v2.0.1)

Put the function inside of across().

rownames_to_column(mtcars, var = "car") %>%
  mutate(across("car", ~str_remove_all(., "\\d")))
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.