Home > Software design >  How to find strings that contain substring in R
How to find strings that contain substring in R

Time:11-14

I have a dataframe with a column "grade" which can be "bad", "moderate", "good" or "very good" and I want all the rows that match "good" or "very good", so I'm trying df[df$grade==".*good"] but to no avail.

What am I doing wrong?

CodePudding user response:

As Wimpel commented, you should use grepl() which uses regular expressions (regex) to look for patterns in string vectors. For your case,

df[grepl('.*good', df$grade), ]

will show you the rows of the data frame you are asking for.

CodePudding user response:

library(tidyverse)

df <- tibble(
  grade = sample(c("bad", "moderate", "good", "very good"), 20, replace = TRUE)
)

df %>% 
  filter(str_detect(grade, "good"))

# A tibble: 9 × 1
  grade    
  <chr>    
1 very good
2 good     
3 very good
4 very good
5 very good
6 good     
7 good     
8 very good
9 very good
  • Related