Why I can't use replace_na() with mutate()

I have this tibble and I want replace all the NA with ""and I can't do it. I have tried several ways. What I am doing wrong?

df
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


df %>%
  mutate_all(~replace_na(., ""))

Error in `mutate()`:
ℹ In argument: `a = (structure(function (..., .x = ..1, .y = ..2, . = ..1) ...`.
Caused by error in `vec_assign()`:
! Cant convert `replace` <character> to match type of `data` <double>.
Run `rlang::last_trace()` to see where the error occurred.

df %>%
    mutate(across(tidyselect::everything,
                  ~replace_na(., "")))

Error in `mutate()`:
ℹ In argument: `across(tidyselect::everything, ~replace_na(., ""))`.
Caused by error in `across()`:
! Predicate must return `TRUE` or `FALSE`, not an integer vector.
Run `rlang::last_trace()` to see where the error occurred.

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

This topic was automatically closed 42 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.