1
votes

Consider the following data set:

id = c(1:10)
test_score = c(55, 66, 91, 37, 55, 78, 81, 83, 69, NA)
data = data.frame(id, test_score)

I want to mutate the data so that all scores over 60 are shown as "pass", but the remaining scores stay the same value. I also have to skip NA values and here is a simplified version of what I have tried so far:

data = data %>% 
    mutate(test_score_new = ifelse(test_score = NA, NA, 
          ifelse(test_score >= 60, "Pass", test_score)))

The "test_score" at the end is where I think I am going wrong, I have had success using similar code for TRUE/FALSE values using ifelse but I am not sure how to "retain" certain values which do not meet the criteria.

1

1 Answers

2
votes

The issue is the test_score = NA, which is an assignment operator and not a comparison ==. Also, with NA, we have is.na

data %>% 
 mutate(test_score_new = ifelse(is.na(test_score), NA, 
        ifelse(test_score >= 60, "Pass", test_score)))