Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R : using filter in a for loop and change the data value

I have a list called ‘col_missing’ like below.

col_missing <- list(
  RAgeCat = c(-1, 8)
  , Married = c(-1, 9)
  , skipmeal = c(-1, 8, 9)
)

and I want to filter the dataframe if the column has above values, and replace them as NA. Below code is working but if it is in a for loop, it is not working.

Working

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

temp <- df %>%  
  filter(skipmeal %in% col_missing$skipmeal)
temp$skipmeal <- NA

Not working

for (col in names(col_missing)) {
  temp <- df %>%
    filter(col %in% col_missing$col)
  temp$col <- NA
}

Could you tell me how to do this in a for loop?

>Solution :

We may do this using across and modify the values without any filtering

library(dplyr)
df <- df %>%
    mutate(across(all_of(names(col_missing)),
     ~ replace(.x, .x %in% col_missing[[cur_column()]], NA)))

In the for loop, temp is getting updated in each iteration. Perhaps it should be created outside Also, use [[ instead of $ i.e. col_missing[[col]] instead of col_missing$col

If we need to filter and create multiple datasets in a list

library(purrr)
imap(col_missing, ~ df %>%
         filter(.data[[.y]] %in% .x) %>%
         mutate(!! .y := NA)
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading