replace NA with 0 using starts_with()
Asked Answered
B

1

8

I am trying to replace NA values for a specific set of columns in my tibble. The columns all start with the same prefix so I am wanting to know if there is a concise way to make use of the starts_with() function from the dplyr package that would allow me to do this.

I have seen several other questions on SO, however they all require the use of specific column names or locations. I'm really trying to be lazy and not wanting to define ALL columns, just the prefix.

I've tried the replace_na() function from the tidyr package to no avail. I know the code I have is wrong for the assignment, but my vocabulary isn't large enough to know where to look.

Reprex:

library(tidyverse)

tbl1 <- tibble(
 id = c(1, 2, 3),
 num_a = c(1, NA, 4),
 num_b = c(NA, 99, 100),
 col_c = c("d", "e", NA)
)

replace_na(tbl1, list(starts_with("num_") = 0)))
Blazer answered 23/6, 2017 at 20:54 Comment(0)
K
12

How about using mutate_at with if_else (or case_when)? This works if you want to replace all NA in the columns of interest with 0.

mutate_at(tbl1, vars( starts_with("num_") ), 
          funs( if_else( is.na(.), 0, .) ) )

# A tibble: 3 x 4
     id num_a num_b col_c
  <dbl> <dbl> <dbl> <chr>
1     1     1     0     d
2     2     0    99     e
3     3     4   100  <NA>

Note that starts_with and other select helpers return An integer vector giving the position of the matched variables. I always have to keep this in mind when trying to use them in situations outside how I normally use them..

In newer versions of dplyr, use list() with a tilde instead of funs():

list( ~if_else( is.na(.), 0, .) )
Kessler answered 23/6, 2017 at 21:25 Comment(2)
Two slight variations - don't know if either are any better: mutate_at(tbd1, vars(starts_with('num_')), funs(replace(., is.na(.), 0))) and mutate_at(tbd1, vars(starts_with('num_')), funs(replace_na(., 0)))Dirichlet
in some newer version of dplyr you should write ~if_else( is.na(.), 0, .) ) instead of the funs()Derick

© 2022 - 2024 — McMap. All rights reserved.