Home » How to Filter a data.table in R (With Examples)

How to Filter a data.table in R (With Examples)

by Erma Khan

You can use the following methods to filter the rows of a data.table in R:

Method 1: Filter for Rows Based on One Condition

dt[col1 == 'A', ]

Method 2: Filter for Rows that Contain Value in List

dt[col1 %in% c('A', 'C'), ]

Method 3: Filter for Rows where One of Several Conditions is Met

dt[col1 == 'A' | col2  10, ]

Method 4: Filter for Rows where Multiple Conditions are Met

dt[col1 == 'A' & col2  10, ]

The following examples show how to use each method in practice with the following data.table in R:

library(data.table)

#create data table
dt table(team=c('A', 'A', 'A', 'B', 'C'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data table
dt

   team points assists rebounds
1:    A     99      33       30
2:    A     90      28       28
3:    A     86      31       24
4:    B     88      39       24
5:    C     95      34       28

Example 1: Filter for Rows Based on One Condition

The following code shows how to filter for only the rows where the value in the team column is equal to ‘A’:

#filter for rows where team is A
dt[team == 'A', ]

   team points assists rebounds
1:    A     99      33       30
2:    A     90      28       28
3:    A     86      31       24

Example 2: Filter for Rows that Contain Value in List

The following code shows how to filter for only the rows where the value in the team column is equal to ‘A’ or ‘C’:

#filter for rows where team is A or C
dt[team %in% c('A', 'C'), ]

   team points assists rebounds
1:    A     99      33       30
2:    A     90      28       28
3:    A     86      31       24
4:    C     95      34       28

Related: How to Use %in% Operator in R (With Examples)

Example 3: Filter for Rows where One of Several Conditions is Met

The following code shows how to filter for only the rows where the value in the team column is equal to ‘A’ or the value in the points column is less than 90:

#filter for rows where team is A or points 
dt[team == 'A' | points  90, ]

   team points assists rebounds
1:    A     99      33       30
2:    A     90      28       28
3:    A     86      31       24
4:    B     88      39       24

Note: The | operator stands for “OR” in R.

Example 4: Filter for Rows where Multiple Conditions are Met

The following code shows how to filter for only the rows where the value in the team column is equal to ‘A’ and the value in the points column is less than 90:

#filter for rows where team is A and points 
dt[team == 'A' & points  90, ]

   team points assists rebounds
1:    A     86      31       24

Note: The & operator stands for “AND” in R.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Filter a Vector in R
How to Remove Rows with Any Zeros in R
How to Remove Empty Rows from Data Frame in R

Related Posts