Filter multiple values on a string column in R using Dplyr - GeeksforGeeks

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.
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
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
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
Picked
R Dplyr
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Replace specific values in column in R DataFrame ?
Loops in R (for, while, repeat)
How to change Row Names of DataFrame in R ?
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
R Programming Language - Introduction
Creating a Data Frame from Vectors in R Programming
K-Means Clustering in R Programming