Olachi Mbara
Olachi Mbara

Reputation: 11

How do I use the ifelse function for multiple values?

credit$EDUCATION <- as.factor(ifelse(credit$EDUCATION == 1, 2, 3, 4, "Graduate_School", "College", "High_School", "Other", "Unknown"))

1 "Graduate_School", 2 "College", 3 "High_School", 4 "Other", Everything else that is not values 1 through 4 becomes Unknown

Upvotes: 1

Views: 49

Answers (1)

akrun
akrun

Reputation: 887088

If we have numeric index, use that for replacement

credit$EDUCATION <- c("Graduate_School", "College", "High_School", "Other")[credit$EDUCATION]
credit$EDUCATION[is.na(credit$EDUCATION)] <- "Unknown"

Upvotes: 2

Related Questions