Hi,
I have this simple df:
source <- data.frame(
stringsAsFactors = FALSE,
URN = c("bbb","ccc","hhh",
"aaa","ddd","eee","fff","ggg","iii"),
QF16 = c("No","No","No",
"No","Yes","Yes","Yes","Yes","Yes"),
QF14 = c(2, 2, 0, 1, 0, 1, 1, 1, 1),
QF70 = c(0, 6, 0, NA, 8, 10, 0, 5, NA),
QF2 = c(0, 4, 0, 3, 2, 0, 5, 5, 5),
QF4 = c(10, 10, 0, 8, 9, 10, 7, 10, 10)
)
Values are incorrect as all 0 should be NAs in some of the variables.
How can I replace them globally (all numeric variables 0-2 or 0-10)?
I think I may use:
mutate(
across(
where(~ is.numeric(.) & all(. <= 2, na.rm = TRUE)),
and
mutate(
across(
where(~ is.numeric(.) & any(. > 5 & .<=10, na.rm = TRUE)),
but I don't know how.
As a result I need QF14, QF70 and QF4 to be fixed like that:
result<- data.frame(
stringsAsFactors = FALSE,
URN = c("bbb","ccc","hhh",
"aaa","ddd","eee","fff","ggg","iii"),
QF16 = c("No","No","No",
"No","Yes","Yes","Yes","Yes","Yes"),
QF14 = c(2, 2, NA, 1, NA, 1, 1, 1, 1),
QF70 = c(NA, 6, NA, NA, 8, 10, NA, 5, NA),
QF2 = c(0, 4, 0, 3, 2, 0, 5, 5, 5),
QF4 = c(10, 10, NA, 8, 9, 10, 7, 10, 10)
)
Can you help?