Filter multiple values on a string column in R using Dplyr
Last Updated :
28 Jul, 2021
In this article we will learn how to filter multiple values on a string column in R programming language using dplyr package.
Method 1: Using filter() method
filter() function is used to choose cases and filtering out the values based on the filtering conditions.
Syntax: filter(df, condition)
Parameters:
df: Dataframe object
condition: filtering based on this condition
Example: R program to filter multiple values using filter()
R
library(dplyr)
df <- data.frame(prep = c(11:15),
str = c("Welcome", "to", "Geeks",
"for", "Geeks"),
date=c("Sunday","Monday", "Thursday",
"January","December"))
filter(df,date=='Sunday'| date=='Monday')
Output:
prep str date
1 11 Welcome Sunday
2 12 to Monday
Method 2: Using filter() with %in% operator
In this, first, pass your dataframe object to the filter function, then in the condition parameter write the column name in which you want to filter multiple values then put the %in% operator, and then pass a vector containing all the string values which you want in the result. This produces all the rows containing the string values in the specified column.
Syntax: filter(df, date %in% Â c("Thursday", "January", "Sunday"))
Parameters:
df: dataframe object
condition: column_name %in% vector string of values
Example: R program to filter multiple values using %in%
R
library(dplyr)
df <- data.frame(prep = c(11:15),
str = c("Welcome", "to", "Geeks",
"for", "Geeks"),
date=c("Sunday","Monday", "Thursday",
"January","December"))
filter(df, date %in% c("Thursday", "January", "Sunday"))
Output:
prep str date
1 11 Welcome Sunday
2 13 Geeks Thursday
3 14 for January
Example: Same as above but in this example, we perform the same operation, but on different columns with a different set of values.Â
R
library(dplyr)
df <- data.frame(prep = c(11:15),
str = c("Welcome", "to", "Geeks",
"for", "Geeks"),
date=c("Sunday","Monday", "Thursday",
"January","December"))
filter(df, str %in% c("Geeks", "to"))
Output:
prep str date
1 12 to Monday
2 13 Geeks Thursday
3 15 Geeks December
Method 3: Using select() method
For this functionality, select() function accepts 2 parameters, first is the filter function and the second is a vector of column names,
Syntax: select(filter(df, condition, columns)
Parameters:
df: dataframe object
condition: filtering condition
columns: vector of column names which you want to print
filter() works almost the same way as given above, the only difference being the vector of column names which we are passing in the second argument. This prints only the columns that were passed in the select function. In this way we can print selected columns only.
Example: Printing selected rows
R
library(dplyr)
df <- data.frame(prep = c(11:15),
str = c("Welcome", "to", "Geeks",
"for", "Geeks"),
date=c("Sunday","Monday", "Thursday",
"January","December"))
select(filter(df, date %in% c("January", "Monday")), c(date,prep))
Output:
date prep
1 Monday 12
2 January 14
Similar Reads
Remove duplicate rows based on multiple columns using Dplyr in R In this article, we will learn how to remove duplicate rows based on multiple columns using dplyr in R programming language. Dataframe in use: lang value usage 1 Java 21 21 2 C 21 21 3 Python 3 0 4 GO 5 99 5 RUST 180 44 6 Javascript 9 48 7 Cpp 12 53 8 Java 21 21 9 Julia 6 6 10 Typescript 0 8 11 Pyth
4 min read
Filter data by multiple conditions in R using Dplyr In this article, we will learn how can we filter dataframe by multiple conditions in R programming language using dplyr package. The filter() function is used to produce a subset of the data frame, retaining all rows that satisfy the specified conditions. The filter() method in R programming languag
3 min read
Drop multiple columns using Dplyr package in R In this article, we will discuss how to drop multiple columns using dplyr package in R programming language. Dataset in use: Drop multiple columns by using the column name We can remove a column with select() method by its column name Syntax: select(dataframe,-c(column_name1,column_name2,.,column_na
4 min read
Filtering row which contains a certain string using Dplyr in R In this article, we will learn how to filter rows that contain a certain string using dplyr package in R programming language. Functions Used Two main functions which will be used to carry out this task are: filter(): dplyr package's filter function will be used for filtering rows based on condition
4 min read
Select variables (columns) in R using Dplyr In this article, we are going to select variables or columns in R programming language using dplyr library. Dataset in use: Select column with column name Here we will use select() method to select column by its name Syntax: select(dataframe,column1,column2,.,column n) Here, data frame is the input
5 min read