I am trying to replace NA in a subset of columns and I want to use tidyverse/dplyr syntax. dplyr v1.0.2
In the following, I want only to replace the NAs with 999 in columns ab,ac but not in ads
tbf <- tibble( ab = c(1,3,NA), ac = c(23,NA,33), d = c(22,22,NA), ads = c('ds', NA, "dwe"))
tbf %>% mutate(across(starts_with('a') & where(is.numeric)), ~replace_na(999))
does not seem to work.
I tried ~replace_na(.x,999)
too. That didn't work either.
Any help is appreciated.
across() returns a tibble with one column for each column in . cols and each function in . fns . if_any() and if_all() return a logical vector.
Replace NA on Multiple Columns by IndexUse tidyr::replace_na() to update NA values with 0 on selected multiple column indexes. dplyr::mutate_at() takes vector with index numbers and replace_na() replaces all NA with 0 on all multiple indexes specified with vector.
replace_na() will not work if the variable is a factor, and the replacement is not already a level for your factor. If this is the issue, you can add another level to your factor variable for 0 before running replace_na(), or you can convert the variable to numeric or character first.
You can replace NA values with zero(0) on numeric columns of R data frame by using is.na() , replace() , imputeTS::replace() , dplyr::coalesce() , dplyr::mutate_at() , dplyr::mutate_if() , and tidyr::replace_na() functions.
The fn
(you are using replace_na
) needs to be inside the across
. You then also need to reference the current column by inserting a .
in the replace_na
. This way you can use the filtering you are proposing in your question (columns starting with "a" and with numeric values) as opposed to the other answers here which specifically use column names.
tbf %>% mutate(across(starts_with('a') & where(is.numeric), ~replace_na(.,999)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With