Your error may be caused by trying to put the character "" into a numeric column. Note the two different results when I limit the replacement to column b, which is text, and when I try to do all the columns.
DF <- tibble::tribble(
~a, ~b, ~c,
NA, NA, NA,
2, "red", NA,
2, NA, NA,
1, NA, NA,
NA, "red", NA,
NA, "red", NA,
2, NA, NA,
3, "blue", NA,
1, "red", NA,
2, NA, NA,
NA, "red", NA,
1, "red", NA,
2, "red", NA,
2, NA, NA,
NA, "blue", NA)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
DF |> mutate(across(.col = "b",.fns = ~replace_na(.x, "")))
#> # A tibble: 15 × 3
#> a b c
#> <dbl> <chr> <lgl>
#> 1 NA "" NA
#> 2 2 "red" NA
#> 3 2 "" NA
#> 4 1 "" NA
#> 5 NA "red" NA
#> 6 NA "red" NA
#> 7 2 "" NA
#> 8 3 "blue" NA
#> 9 1 "red" NA
#> 10 2 "" NA
#> 11 NA "red" NA
#> 12 1 "red" NA
#> 13 2 "red" NA
#> 14 2 "" NA
#> 15 NA "blue" NA
DF |> mutate(across(.col = everything(),.fns = ~replace_na(.x, "")))
#> Error in `mutate()`:
#> ℹ In argument: `across(.col = everything(), .fns = ~replace_na(.x,
#> ""))`.
#> Caused by error in `across()`:
#> ! Can't compute column `a`.
#> Caused by error in `vec_assign()`:
#> ! Can't convert `replace` <character> to match type of `data` <double>.
#> Backtrace:
#> ▆
#> 1. ├─dplyr::mutate(...)
#> 2. ├─dplyr:::mutate.data.frame(...)
#> 3. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#> 4. │ ├─base::withCallingHandlers(...)
#> 5. │ └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#> 6. │ ├─base::withCallingHandlers(...)
#> 7. │ └─mask$eval_all_mutate(quo)
#> 8. │ └─dplyr (local) eval()
#> 9. ├─tidyr::replace_na(a, "")
#> 10. ├─tidyr:::replace_na.default(a, "")
#> 11. │ └─vctrs::vec_assign(data, missing, replace, x_arg = "data", value_arg = "replace")
#> 12. └─vctrs (local) `<fn>`()
#> 13. └─vctrs::vec_default_cast(...)
#> 14. ├─base::withRestarts(...)
#> 15. │ └─base (local) withOneRestart(expr, restarts[[1L]])
#> 16. │ └─base (local) doWithOneRestart(return(expr), restart)
#> 17. └─vctrs::stop_incompatible_cast(...)
#> 18. └─vctrs::stop_incompatible_type(...)
#> 19. └─vctrs:::stop_incompatible(...)
#> 20. └─vctrs:::stop_vctrs(...)
#> 21. └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)
Created on 2023-06-27 with reprex v2.0.2