How to replace NA with 0 using starts_with()

0 votes

I am trying to replace NA values with 0 for a specific set of columns in my tibble. All the columns start with the same prefix. So, I want 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 tried replace_na() function from the tidyr package to no avail. Below is the code.

library(tidyverse)

tibble1 <- tibble(
 id = c(10, 20, 30),
 col_a = c(1, NA, 4),
 col_b = c(NA, 99, 100),
 col_c = c("d", "e", NA)
)
replace_na(tbl1, list(starts_with("num_") = 0)))
Apr 3, 2018 in Data Analytics by BHARANI
• 410 points
81 views

1 answer to this question.

0 votes

Well I could suggest various options such as mutate_at with if_else(or case_when). This works when you want to replace all the NA values in the columns with 0.

mutate_at(tibble1, vars( starts_with("col_") ), 
          funs( if_else( is.na(.), 0, .) ) )

# A tibble: 3 x 4
     id col_a col_b col_c
  <dbl> <dbl> <dbl> <chr>
1     10     1     0     d
2     20     0    99     e
3     30     4   100    <NA>

Note: starts_with and other select helpers return an integer vector giving the position of the matched variables.

answered Apr 3, 2018 by darklord
• 6,190 points

Related Questions In Data Analytics

0 votes
1 answer

How to replace NA values in a dataframe with Zero's ?

It is simple and easy: df1<-as.data.frame(matrix(sample(c(NA, 1:10), 100, ...READ MORE

answered Apr 10, 2018 in Data Analytics by CodingByHeart77
• 3,690 points
167 views
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,690 points
217 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE) ...READ MORE

answered Aug 14 in Data Analytics by anonymous
107 views
0 votes
1 answer

With the help of tidyverse: how to rename a column to a variable name

With the help of Dplyr: rename function ...READ MORE

answered Apr 3, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Apr 3, 2018 by DeepCoder786 50 views
0 votes
4 answers

How to remove NA values with dplyr::filter()

Can we create a alist as below ...READ MORE

answered Aug 5 in Data Analytics by anonymous
10,775 views
0 votes
1 answer
0 votes
1 answer

How can I use parallel so that it preserves the list of data frames

You can use pmap as follows: nc <- ...READ MORE

answered Apr 4, 2018 in Data Analytics by kappa3010
• 2,070 points
45 views
0 votes
1 answer
0 votes
1 answer

How to order data frame rows according to vector with specific order using R?

You can try using match: data <- data.frame(alphabets=letters[1:4], ...READ MORE

answered Apr 30, 2018 in Data Analytics by darklord
• 6,190 points
112 views
0 votes
1 answer

How to change y axis max in time series using R?

The axis limits are being set using ...READ MORE

answered Apr 3, 2018 in Data Analytics by darklord
• 6,190 points
223 views