Search code examples
rvariablesdplyrtidyversedata-cleaning

How to select rows starting with a particular string in R?


I have data like so:

df <- data.frame(name = c("James", "jonathan", "Abel", "Cynthia", "Cornelius", "alex"))

      name
     James
  jonathan
      Abel
   Cynthia
 Cornelius
      alex

I want to select rows where name doesn't begin with "A" or "J". Expected result:

      name
   Cynthia
 Cornelius

I'd like a simple dplyr solution.


Solution

  • You can use grepl with filter. "^[A|J]" matches strings that start with an A or a J, while ignore.case = TRUE indicates that both lower and uppercases letters are matched. Since you want to keep the value that do not start with A or J, you can use ! to invert the selection:

    library(dplyr)
    df |>
      filter(!grepl("^[A|J]", name, ignore.case = TRUE))
    
    #        name
    # 1   Cynthia
    # 2 Cornelius