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 373 views

## 1 answer to this question.

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
• 6,320 points

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

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

+1 vote

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

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

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

## How to use a function to repeat a set of procedures on specific set of columns in a data frame?

You can parse the strings to symbols. ...READ MORE

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

You can use pmap as follows: nc <- ...READ MORE
{"ft_lang_label":"__label__en","ft_lang_prob":0.798212,"math_prob":0.9632295,"size":537,"snap":"2020-45-2020-50","text_gpt3_token_len":165,"char_repetition_ratio":0.10694184,"word_repetition_ratio":0.0,"special_character_ratio":0.33147115,"punctuation_ratio":0.15126051,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98111093,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-24T18:39:31Z\",\"WARC-Record-ID\":\"<urn:uuid:150bd8a3-1843-4a47-b826-2a7648529454>\",\"Content-Length\":\"146937\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:19c28ef8-974b-416b-a694-f94563461990>\",\"WARC-Concurrent-To\":\"<urn:uuid:62aaac6b-fe78-4ae9-8602-6ac0a634700d>\",\"WARC-IP-Address\":\"52.85.146.93\",\"WARC-Target-URI\":\"https://www.edureka.co/community/625/how-to-replace-na-with-0-using-starts-with\",\"WARC-Payload-Digest\":\"sha1:NF2POS3MLD65PA3XPDBLELDIUXTKZH45\",\"WARC-Block-Digest\":\"sha1:CMQ4HW3VYNTYLKHVNNWNWPOCYCAUXRXD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141176922.14_warc_CC-MAIN-20201124170142-20201124200142-00282.warc.gz\"}"}