If then do A else do B — ifelse function in R & Exploratory

Kan Nishida
learn data science
Published in
4 min readJul 11, 2020

--

Most likely you have used or heard about ‘ifelse’ function before. It’s everywhere such as Excel, database, etc.

And of course, it is in R, which means you can use it in Exploratory as well.

I’m going to talk about how you can use the ifelse function in Exploratory.

Basics

The ifelse function takes 3 arguments.

ifelse(a condition, a return value when the condition is TRUE, a return value when the condition is FALSE)

Example 1 — Greater Than $5000 or Not

Let’s take a look at the very basic scenario.

We have this employee data, each row represents each employee.

Now, we want to label the employees whose Monthly Income is greater than 10,000 as “High Income” and the rest of the employees as “Low Income”.

Select ‘Create Calculation (Mutate)’ from the column header menu of the ‘Monthly Income’ column.

And type the following calculation.

ifelse(MonthlyIncome  >= 10000, "High Income", "Low Income")

Once you run it you’ll get a new column with the labels.

You can see that there are 281 employees that match the condition (or make more than $10,000 income).

Example 2 — Use a Function inside ‘ifelse’

There are 9 job roles in this data.

And we want to find the employees whose job role names contain ‘Sales’ and label them ‘Sales People’, and label the others as ‘Non-Sales People’.

For this, we can use str_detect function, which would return TRUE when it finds a given set of letters.

Select ‘Create Calculation (Mutate)’ from the column header menu of ‘Job Role’.

And type the following calculation.

ifelse(str_detect(JobRole, "Sales"), "Sales People", "Non-Sales People")

And again, when we go to the Summary view we can see that there are 409 employees whose job role names contain ‘Sales’.

Example 3 — Multiple Values to Match

Sometimes, you might want to create a condition that would match one of the listed values.

As we have seen above, there are 9 job roles.

Let’s say we want to label as “High Level Jobs” for the employees whose job roles are one of the following.

  • Sales Executive
  • Research Director
  • Manager

And label others as “Others”.

We can write a condition like below for this requirement.

JobRole %in% c("Sales Executive", "Research Director", "Manager"

%in% is a conditional operator that can be used for a condition of 'equal to one of the values'. By the way, if you want to do the opposite - not equal to any of the values - then you can use %nin%.

After the %in% operator you want to give a list. In R, since everything needs to be presented as a function you want to use c function, which stands for 'concatenate', to create a list.

Select ‘Create Calculation (Mutate)’ from the column header menu of ‘Job Role’.

Select ‘%in%’ from the suggested list.

And type the following.

c("Sales Executive", "Research Director", "Manager")

Then, complete the whole ifelse function as follows.

ifelse(JobRole  %in% c("Sales Executive", "Research Director", "Manager"), "High Level Job", "Others")

Once you run it, you’ll have a new column created with the labels.

Multiple Conditions

ifelse function can take just one condition, but what if you want to create multiple functions?

You can use case_when function, and I'd suggest you take a look at this how-to note.

Try it for Yourself!

If you want to quickly try it out and you don’t have an Exploratory account yet, sign up from our website for 30 days free trial without a credit card!

If you happen to be a current student or teacher at schools, it’s free! Sign up for Community Plan.

And, if you don’t mind sharing what you create publicly you can sign up for Public edition of Exploratory (FREE)!

--

--

CEO / Founder at Exploratory(https://exploratory.io/). Having fun analyzing interesting data and learning something new everyday.