Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using replace_na with across in mutate

Tags:

r

dplyr

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.

like image 781
user14306090 Avatar asked Sep 19 '20 15:09

user14306090


People also ask

What does across () do in R?

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.

How do I replace Na in multiple columns in R?

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.

Why is Replace_na not working?

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.

How do I replace Na with 0 in a column in R?

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.


1 Answers

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)))
like image 163
juljo Avatar answered Oct 05 '22 23:10

juljo