replace NA with 0 using starts_with()

给你一囗甜甜゛ 提交于 2019-12-08 18:27:14

问题


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)))

回答1:


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..



来源:https://stackoverflow.com/questions/44729796/replace-na-with-0-using-starts-with

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!