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…
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"))