Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading