I am trying to remove some NA's in one variable depending on the values gotten in a previous variable but I haven't managed to succeed...
Thanks in advance!
There are two variables: info_geo
and info_geo_actzd
. If the answer in the former one is "No", I want the answers in info_geo_actzd
to change from "NA" to "No". The rest of the answers I want to leave them the same as now
I have tried various ways but I can't do it properly
db <- db %>%
mutate(info_geo_actzd = case_when(
info_geo == 'No' ~ "No",
TRUE ~ .))
db <- db %>%
mutate(info_geo_actzd=ifelse('info_geo' == 'No' & 'info_geo_actzd' == 'NA', No))
I've also tried this but no result
db <- db %>%
mutate(info_geo_actzd = case_when(
any(info_geo == 'No') ~ 'NA',
TRUE ~ .))
Thanks in advance
CodePudding user response:
The .
will return the dataset instead of the column. If the NA
s are unquoted, use is.na
to create a logical vector and assign those to "No"
or else (TRUE
) return the original column info_geo
library(dplyr)
db <- db %>%
mutate(info_geo_actzd = case_when(
is.na(info_geo) ~ "No",
TRUE ~ info_geo))
For more than one column, loop over the columns (either column index or column name - unquoted or quoted, or if there are any patterns in column names, wrap those in select-helpers -contains/matches/starts_with/ends_with
etc)
db <- db %>%
mutate(across(starts_with("info"), ~ case_when(is.na(.x) ~ "No",
TRUE ~ .x), .names = "{.col}_actzd"))