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
• 400 points
33 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,140 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,680 points
75 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,680 points
61 views
+1 vote
1 answer

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

You have missed out on "na.rm=TRUE" inside ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
36 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,700 points

edited Apr 3, 2018 by DeepCoder786 25 views
0 votes
3 answers

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

Null values have no notion of equality ...READ MORE

answered Apr 11 in Data Analytics by Zane
3,307 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,010 points
20 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,140 points
43 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,140 points
55 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.