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
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?
CodePudding user response:
We may do this using across
and modify the values without any filter
ing
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)
)