2.4 C
London
Friday, December 20, 2024
HomeStatistics TutorialRR: How to Select Rows in Data Frame Based on Values in...

R: How to Select Rows in Data Frame Based on Values in Vector

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 one of the following methods to select rows from a data frame in R based on values in a vector:

Method 1: Use Base R

new_df %in% values_vector, ]

Method 2: Use dplyr Package

library(dplyr)

new_df % filter(column_name %in% values_vector)

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

#create data frame
df frame(division=c('West', 'West', 'East', 'East', 'North'),
                 points=c(120, 100, 104, 98, 105),
                 assists=c(30, 35, 64, 28, 23))

#view data frame
df

  division points assists
1     West    120      30
2     West    100      35
3     East    104      64
4     East     98      28
5    North    105      23

Example 1: Use Base R to Select Rows Based on Values in Vector

We can use the following code to select only the rows from the original data frame where the value in the division column is equal to ‘West’ or ‘North.’

#define values of interest
my_values West', 'North')

#select rows that contain 'West' or 'North' in division column
new_df #view results
new_df

  division points assists
1     West    120      30
2     West    100      35
5    North    105      23

The new data frame only contains the rows where the value in the division column is equal to ‘West’ or ‘North.’

Example 2: Use dplyr to Select Rows Based on Values in Vector

We can also use the filter() function from the dplyr package in R select only the rows from the original data frame where the value in the division column is equal to ‘West’ or ‘North.’

library(dplyr)

#define values of interest
my_values West', 'North')

#select rows that contain 'West' or 'North' in division column
new_df % filter(division %in% my_values)

#view results
new_df

  division points assists
1     West    120      30
2     West    100      35
3    North    105      23

The new data frame only contains the rows where the value in the division column is equal to ‘West’ or ‘North.’

Note: The base R and dplyr methods produce the same results. However, the dplyr method will tend to be faster when working with extremely large data frames.

Additional Resources

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

How to Select Random Rows in R Using dplyr
How to Select Rows by Condition in R
How to Select Rows Where Value Appears in Any Column 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