Filter multiple values on a string column in R using Dplyr
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)
df: Dataframe object
condition: filtering based on this condition
Example: R program to filter multiple values using filter()
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”))
df: dataframe object
condition: column_name %in% vector string of values
Example: R program to filter multiple values using %in%
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.
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)
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
date prep 1 Monday 12 2 January 14