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

Grapping \n1 with grepl in r

I have a vector of strings either starting with \n1. or \ntext and I wish to filter all those starting with \n1.
Sample:

[1] "\n1. Morgenhanen matter"
[2] "\n1. Morgenstund har guld"
[3] "\nMorgensange for børn be"

but I can’t seem to grap those sentences starting with \n1. Here’s where I’m at:

grepl("^['\\\\']n1", df$text)

but it returns false for all sentences…

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

In the end I want to end up with something like

library(tidyverse)

df %>% 
   filter(those sentences starting with \n1)

I’m sorry, I’m just not the best at regex in r…

>Solution :

You could do:

library(dplyr)

df %>%
  filter(df, grepl("^\\n1", text))

Output:

                       text
1   \n1. Morgenhanen matter
2 \n1. Morgenstund har guld

Data

df <- data.frame(text = c("\n1. Morgenhanen matter", 
                      "\n1. Morgenstund har guld", 
                      "\nMorgensange for børn be"))
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