df <- data.frame(CAR = c(1,1,3,9,1),
BIKE = c(2,NA,4,NA,9),
PLANE = c(8,NA,6,7,9),
BOAT = c(1,2,NA,4,NA),
SCOOTER = c(2,3,6,9,NA))
Hi, I have a df like this. I will like to replace NA values across every row where ‘CAR’ has a value of 1 only; so you get something like this.
NEW_df <- data.frame(CAR = c(1,1,3,9,1),
BIKE = c(2,0,4,NA,9),
PLANE = c(8,0,6,7,9),
BOAT = c(1,2,NA,4,0),
SCOOTER = c(2,3,6,9,0))
I know to replace NA’s across the whole dataset, but cant get around this. Any help on this please. I have tried:
NEW_DF <- df %>% mutate(across(c(-CAR), ~replace(.,CAR == 1, NA)))