WebR Charlie Data Example

Conditional Filtering

So far, what we’ve done in R is also easy to do in Excel/Google Sheets, but now we’ll start getting into things that are easier in R than they are in Excel/Google Sheets.

Filtering

Filtering is a way to subset a dataset by rows and keep only the rows that match a certain value. To do this, we use the filter() function. Let’s filter to just see which treats Charlie likes best, so where the ‘yummy’ column is equal to 5.

In R, when you want to check to see if something matches a value you will use ==:

You can also use >, <, or combine them with = to get values greater/less than and equal to (<= or <=):

Note: It’s easy to mix up filter() and select(). A good way to remember is that filteR() acts on Rows, while seleCt() acts on Columns.

Even if Charlie likes a treat, he doesn’t always finish them. We can use filter() on non-numerical columns to see which treats don’t go to waste, so where ‘thrown_out’ equals “no”.

What if we wanted to find all the treats that are thrown out, even including sometimes? There are two ways to do this using logical operators. Logical operators are just symbols that help us make comparisons, we already have learned some, but here are more:

  • == : equal to (matches)

  • != : does not equal (pronounced as the “not” operator)

  • & : the AND operator, but conditions are true

  • | : the OR operator, either condition is true (this is probably on the right side of your keyboard above \)

So if we want to include both “no” and “sometimes” in our output there are a couple ways:

Here you should see the same table print out for each line of code.

Sometimes using & and | can be confusing. The issue is that in normal speech “and” is what combines things and allows for more options, whereas “or” usually just means one or the other and is more picky, but in coding it’s the opposite-& is more exclusive and | is more inclusive. You can always try each one and see what the output is.