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)))
mutate_at(tbd1, vars(starts_with('num_')), funs(replace(., is.na(.), 0)))
andmutate_at(tbd1, vars(starts_with('num_')), funs(replace_na(., 0)))
– Dirichlet