R dplyr::case_when()

  • used to create a vector from if-else
  • part of the dplyr package
  • designed to be used within the context of data frames, particularly columns
  • similar to the SQL CASE WHEN-THEN statements



  • condition_1: condition_n: TRUE/ FALSE
  • Default: optional if none of the conditions are TRUE

Example 1

Example 2:

For the example below, we will use the mpg data. We are going to create a new table and in that table, we are going to classify the vehicles into automatic or manual and the number of cars in that group

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top