Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Statement Equivalent in R

Tags:

r

case

I have a variable in a dataframe where one of the fields typically has 7-8 values. I want to collpase them 3 or 4 new categories within a new variable within the dataframe. What is the best approach?

I would use a CASE statement if I were in a SQL-like tool but not sure how to attack this in R.

Any help you can provide will be much appreciated!

like image 963
Btibert3 Avatar asked Jan 07 '11 02:01

Btibert3


People also ask

Is there a case statement in R?

Switch case statements are a substitute for long if statements that compare a variable to several integral values. Switch case in R is a multiway branch statement. It allows a variable to be tested for equality against a list of values.

How do you do a case statement in R?

Case when in R can be executed with case_when() function in dplyr package. Dplyr package is provided with case_when() function which is similar to case when statement in SQL. case when with multiple conditions in R and switch statement.

What does Case_when mean in R?

case_when: A general vectorised if This function allows you to vectorise multiple if and else if statements. It is an R equivalent of the SQL CASE WHEN statement.

Does R have a switch statement?

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.


2 Answers

case_when(), which was added to dplyr in May 2016, solves this problem in a manner similar to memisc::cases().

As of dplyr 0.7.0, for example:

mtcars %>%    mutate(category = case_when(     cyl == 4 & disp < median(disp) ~ "4 cylinders, small displacement",     cyl == 8 & disp > median(disp) ~ "8 cylinders, large displacement",     TRUE ~ "other"   ) ) 

Original answer

library(dplyr) mtcars %>%    mutate(category = case_when(     .$cyl == 4 & .$disp < median(.$disp) ~ "4 cylinders, small displacement",     .$cyl == 8 & .$disp > median(.$disp) ~ "8 cylinders, large displacement",     TRUE ~ "other"   ) ) 
like image 50
Evan Cortens Avatar answered Sep 27 '22 15:09

Evan Cortens


Have a look at the cases function from the memisc package. It implements case-functionality with two different ways to use it. From the examples in the package:

z1=cases(     "Condition 1"=x<0,     "Condition 2"=y<0,# only applies if x >= 0     "Condition 3"=TRUE     ) 

where x and y are two vectors.

References: memisc package, cases example

like image 41
Henrico Avatar answered Sep 27 '22 17:09

Henrico