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
• 420 points
1,658 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 Sahiti
• 6,370 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,750 points
2,062 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,750 points
4,556 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, 2019 in Data Analytics by anonymous
1,937 views
+1 vote
5 answers

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

Try this: df %>% filter(!is.na(col1)) READ MORE

answered Mar 26, 2019 in Data Analytics by anonymous
331,392 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,090 points
1,071 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 Sahiti
• 6,370 points
7,464 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 Sahiti
• 6,370 points
3,832 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP