15.1 C
London
Friday, July 5, 2024
HomeStatistics TutorialRHow to Filter a data.table in R (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories