I have a df like this:
NEW_df <- data.frame(CAR = c("YES","NO","NO","YES","NO"),
BIKE = c(2,0,4,NA,9),
PLANE = c(8,0,6,7,9),
BOAT = c(NA,NA,NA,4,3),
SCOOTER = c(2,3,6,9,0))
I will like to replace NA's in column BOAT with '0'if the column CAR is 'NO', so I can have something like this:
NEW_df <- data.frame(CAR = c("YES","NO","NO","YES","NO"),
BIKE = c(2,0,4,NA,9),
PLANE = c(8,0,6,7,9),
BOAT = c(NA,0,0,4,3),
SCOOTER = c(2,3,6,9,0))
I thought something like this, but it didnt work:
NEW_df %>%
mutate(across(BOAT, ~ if_else(CAR == NO & is.na(.), 0, .)))
Is it even possible to place the replaced values in a new column too.