1. Changing and creating variables with case_when()
In this lesson, we'll learn how to create a new categorical variable based on the values of other variables.
To start, let’s take the case of when we have a single variable or vector.
2. case_when()
Here, we’ve got a vector, x, that is the numbers 1 through 20. Let’s say we want to change it so that if x is divisible by 15, it’s “fizz buzz”, if it’s divisible by 3, it’s “fizz”, and if it’s divisible by 5, it’s “buzz.” If it’s not divisible by any of those numbers, we just make it a character. How can we write code to do this?
We can use a useful dplyr function case_when(). case_when() is a series of statements. The first part of each statement is the condition to check. If it’s true, the second part of the statement, after the tilde, tells us what to change x to. We check the conditions in order, and finally, we can use TRUE as the last statement to check. This evaluates TRUE for every x, so in other words, it will change any remaining xs based on what’s after the final tilde.
3. Order matters
It’s important to know that order matters for case_when(), just like it does with if_else()! So you want to go from the most specific to the most general.
For example, let’s say you had the following code, where you accidentally checked the same condition twice but wanted to return “fuzzy buzz” instead of “fizz” if x is divisible by 3. case_when() evaluates the value of x, checking the conditions in order, and as soon as a condition is true, it changes the value of x. It won’t go on to check the rest of the conditions. Therefore, we never see “fuzzy buzz”!
4. case_when() with multiple variables
Now let’s use case_when() to create a new variable for a dataset. In this case, we have two variables: someone's mood and whether they know it or not. We want to make a new variable, the action they should take, based on these. If they're happy and they know it, they should clap their hands. If they're happy and they don't know it, they should stomp their feet. If they're sad, they should look at puppies. Finally, everyone else should jump around.
5. Let's practice!
Let's try it out!