Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Data Science
  3. Question
Data Science

Filtering row which contains a certain string using dplyr

Asked by Launa Kirchner Jul 15, 2021 3.2K views 3 answers
Share

About this question

 I have to filter a data frame using as criterion those row in which is contained the string RTB. I'm using dplyr.

d.del <- df %.%
  group_by(TrackingPixel) %.%
  summarise(MonthDelivery = as.integer(sum(Revenue))) %.%
  arrange(desc(MonthDelivery))

I know I can use the function filter in dplyr but I don't exactly know how to tell it to check for the content of a string.

In particular, I want to check the content in the column TrackingPixel. If the string contains the label RTB I want to remove the row from the result.

Your answer

3 Answers

Lilliansikes Latest answer

Answered on Mar 18, 2025

Great support from Marble Run 3D. Thanks a lot.

Was this helpful?

Ranjana Admin JanBask Expert

Answered on Jun 24, 2024

In R, particularly when using the dplyr package, you can filter rows based on whether a certain string is present in a column. Here’s how you can achieve this:

Example Data

Let's assume you have a data frame called df with columns ID and Text. Here’s a sample data frame:

  library(dplyr)# Sample datadf &lt;- data.frame(  ID = c(1, 2, 3, 4, 5),  Text = c("apple", "banana", "orange", "grape", "kiwi"))

Output:

mathematica

    ID   Text1  1  apple2  2 banana3  3 orange4  4  grape5  5   kiwi

Filtering Rows Containing a Certain String

  Now, let's filter rows where the Text column contains the string "apple".# Filter rows containing "apple" in the Text columnfiltered_df &lt;- df %>%  filter(grepl("apple", Text, ignore.case = TRUE))print(filtered_df)Output:mathematicaCopy code  ID  Text1  1 appleExplanationfilter(grepl("apple", Text, ignore.case = TRUE)):grepl("apple", Text, ignore.case = TRUE) checks if the string "apple" is present in each element of the Text column (ignore.case = TRUE makes the search case-insensitive).

filter() function from dplyr filters rows based on the condition provided inside it.

Notes:

Case Sensitivity: The grepl() function used inside filter() checks for a match without considering case due to ignore.case = TRUE. If you need case-sensitive matching, omit ignore.case = TRUE.

Multiple Matches: If you want to filter based on multiple strings or more complex conditions, you can modify the grepl() call accordingly within the filter() function.

This approach allows you to filter rows in your data frame based on whether a certain string (or pattern) exists within a specific column using dplyr in R. Adjust the string and column names as per your actual data structure and filtering needs.

Was this helpful?

More Data Science discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Data Science Blogs

Guides, tips and career advice on Data Science from JanBask experts.