Difficulties with mutate across all columns and fct_recode

Hello everyone,

i am having difficulties with some code that was working 6 months ago, and it fails silently now. I have tried reading the tidyverse documentation, but i am still not figuring it out.

df <- df %>% 
  mutate(across(everything(), ~ fct_recode(.x,
                                           "Strongly Agree"            = "5",
                                           "Agree"                     = "4",
                                           "Neither"                   = "3",
                                           "Disagree"                  = "2",
                                           "Strongly Disagree"         = "1"
  )))

where is the mistake? I mean, some suggested that across is deprecated but the documentation is still live, i wouldn't know where to begin.

Thanks!
George

theres no mistake unless the contents of your df are inappropriate; like having non factor columns that you then attempt to recode. also if it fails it would fail loudly unless you have altered how your R session responds to warnings and errors...

heres a small df, that shows a case where it would work

df <- data.frame(a=factor(1:5),
                 b=factor(5:1))

results in

                 a                 b
1 Strongly Disagree    Strongly Agree
2          Disagree             Agree
3           Neither           Neither
4             Agree          Disagree
5    Strongly Agree Strongly Disagree
>
1 Like

Thank you for the answer, it works. And I also think i found a culprit, namely package plyr that loaded after tidyverse creating some weird behavior. I think this thread can be closed or deleted.

Again, thank you!

My guess is that you may have been trying to aplpy fct_recode() to a numeric column rather than experiencing any issues with plyr:

library(tidyverse)
# using character column
df <- tibble(x = 1:5 |> as.character())
df %>% 
  mutate(across(everything(), ~ fct_recode(.x,
                                           "Strongly Agree"            = "5",
                                           "Agree"                     = "4",
                                           "Neither"                   = "3",
                                           "Disagree"                  = "2",
                                           "Strongly Disagree"         = "1"
  )))
#> # A tibble: 5 × 1
#>   x                
#>   <fct>            
#> 1 Strongly Disagree
#> 2 Disagree         
#> 3 Neither          
#> 4 Agree            
#> 5 Strongly Agree
# using factor column
df <- tibble(x = 1:5 |> factor())
df %>% 
  mutate(across(everything(), ~ fct_recode(.x,
                                           "Strongly Agree"            = "5",
                                           "Agree"                     = "4",
                                           "Neither"                   = "3",
                                           "Disagree"                  = "2",
                                           "Strongly Disagree"         = "1"
  )))
#> # A tibble: 5 × 1
#>   x                
#>   <fct>            
#> 1 Strongly Disagree
#> 2 Disagree         
#> 3 Neither          
#> 4 Agree            
#> 5 Strongly Agree
# using numeric column
df <- tibble(x = 1:5)
df %>% 
  mutate(across(everything(), ~ fct_recode(.x,
                                           "Strongly Agree"            = "5",
                                           "Agree"                     = "4",
                                           "Neither"                   = "3",
                                           "Disagree"                  = "2",
                                           "Strongly Disagree"         = "1"
  )))
#> Error in `mutate()`:
#> ℹ In argument: `across(...)`.
#> Caused by error in `across()`:
#> ! Can't compute column `x`.
#> Caused by error in `fct_recode()`:
#> ! `.f` must be a factor or character vector, not an integer vector.
#> Backtrace:
#>      ▆
#>   1. ├─df %>% ...
#>   2. ├─dplyr::mutate(...)
#>   3. ├─dplyr:::mutate.data.frame(...)
#>   4. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#>   5. │   ├─base::withCallingHandlers(...)
#>   6. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#>   7. │     ├─base::withCallingHandlers(...)
#>   8. │     └─mask$eval_all_mutate(quo)
#>   9. │       └─dplyr (local) eval()
#>  10. └─forcats::fct_recode(...)
#>  11.   └─forcats:::check_factor(.f)
#>  12.     └─cli::cli_abort(...)
#>  13.       └─rlang::abort(...)

Created on 2024-08-06 with reprex v2.0.2

I would think the plyr explanation works; I would expect it potentially overwrite dplyr::mutate with plyr::mutate, and therefore not understand accross etc...

Could be, but I was going on was:

and

but you're right — without the actual error messages, we're left to guess

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.