1

I have a dataframe like this,

DATA <- data.frame(
    CARS = c(NA, 66, NA, NA, 74, NA, NA, NA),
    EYE_SIGHT= c("GOOD", "EXCELLENT", "POOR", "POOR", "GOOD", "POOR", "EXCELLENT", "GOOD"))

and I want to create a new column (“OUTCOME”) and tell R, If EYE_SIGHT is "GOOD" or "EXCELLENT” then "OUTCOME" should be ‘1’ for everywhere there is a value in “CARS”. However, if the corresponding value in “CARS” is ‘NA’, then "OUTCOME" will be ‘NA’.

Also, if EYE_SIGHT is “POOR” when “CARS” is NA, then "OUTCOME" should be ‘0’

So, I can have something like this:

   CARS   EYE_SIGHT  OUTCOME
1   NA      GOOD      NA
2   66 EXCELLENT       1
3   NA      POOR       0
4   NA      POOR       0
5   74      GOOD       1
6   NA      POOR       0
7   NA EXCELLENT      NA
8   NA      GOOD      NA

I am not sure how to run this. Any idea will be appreciated.

1

1 Answer 1

1

case_when() from the {{dplyr}} package is a good way to handle multiple if statements.

library(dplyr)
DATA %>%
  mutate(OUTCOME = case_when(
    EYE_SIGHT == "POOR" ~ 0,
    EYE_SIGHT %in% c("EXCELLENT", "GOOD") & !is.na(CARS) ~ 1,
    TRUE ~ NA_real_
  ))

  CARS EYE_SIGHT OUTCOME
1   NA      GOOD      NA
2   66 EXCELLENT       1
3   NA      POOR       0
4   NA      POOR       0
5   74      GOOD       1
6   NA      POOR       0
7   NA EXCELLENT      NA
8   NA      GOOD      NA
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.