R Tutorial: Helpers for filtering

Learn three data.table helpers that make filtering easier: %like% for pattern matching on text, %between% for numeric ranges, and %chin% as a faster alternative to %in% for character vectors.
Chapters
In this lesson we will look at three functions that make subsetting operations far more convenient in data tables. A commonly occurring subsetting operation is to look for all rows that match a pattern in a column. Datatable provides a convenience function percent like percent that is concise and also makes the actual operation easier to understand. In this example, we are looking for all the rows where startstation column starts with the string San Francisco the meta character Carray specifies that you are looking for a pattern at the beginning of a string. Percentbetweenpercent is a helper function that works on numeric columns. It searches for all values in the closed interval VAL1 and VAL2. That is, it finds all values that are greater than OR equal to VAL1 and less than or equal to VAL2. It is also internally optimized to run in parallel wherever possible. Here we subset all rows where duration is between 2000 and 3000%. Ch in percent is another helper function, which is a much more efficient version of percent in percent, but only for character vectors. You can use it to look for specific strings in a vector. For example, here we subset all rows where start station is either Japantown, Mezzes park or MLK Library. Go ahead and practice using these.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







