rdplyrtidyversemutateacross

Problems with mutate(), across (), and Winsorize ()


I am trying to winsorize many (not all) columns in a dataframe using mutate(), across(), and Winsorize() function from DescTools package. I am getting the following error message -

Error in `mutate()`:
ℹ In argument: `across(...)`.
Caused by error in `across()`:
! Can't compute column `drat`.
Caused by error in `Winsorize()`:
! unused arguments (na.rm = TRUE, probs = c(0.01, 0.99))
Backtrace:
 1. mtcars %>% ...
 3. dplyr:::mutate.data.frame(...)
 4. dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
 6. dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
 8. mask$eval_all_mutate(quo)
 9. dplyr (local) eval()

I tried to fix it many ways, but I am unsuccessful. I am using R 4.4.1, dplyr 1.1.4, DescTools 0.99.58. Below is my code -

library(tidyverse)
library(DescTools)

mtcars = as_tibble(mtcars)
mtcars = mtcars %>% 
  mutate(across(c(drat, qsec), ~Winsorize(., na.rm = TRUE, probs = c (0.01, 0.99))))

Solution

  • The arguments for probs and na.rm are meant to be called as part of the quantile() function when using the val argument in Winsorize(). Try this:

    mtcars |>
      mutate(across(c(drat, qsec), ~Winsorize(., val = quantile(., probs = c(0.01, 0.99), na.rm = TRUE))))