Skip to content
Related Articles

Related Articles

Improve Article

Filter multiple values on a string column in R using Dplyr

  • Last Updated : 28 Jul, 2021
Geek Week

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 %inc("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 %inc("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



My Personal Notes arrow_drop_up
Recommended Articles
Page :