-1
library(tidyverse)

Here is the representation of my dataset:

mydata<-data.frame(NCT_number=c(" 1111","  2222","0000","3333","4444 ")

I want to spots characters that are preceded or followed by blanks: One space, two spaces and so on. I tried this:

mydata%>%filter(str_detect(NCT_number,c(" " "|"  " ")))

My expected result is:

  NCT_number
1       1111
2       2222
5      4444 

But it didn't works.

Seydou GORO
  • 1,147
  • 7
  • 13
  • Could you please make the question reproducible, provide some sample data: paste the output of `dput(head(mydata$NCT_number, 10))` into the question, as a minimum and included your expected outcome. – Peter Nov 30 '21 at 15:51
  • Use the backslash to escape it. `\"` –  Nov 30 '21 at 15:58
  • 1
    Can you please update the title as in its present form it is far from reflecting the actual content of your question – Chris Ruehlemann Nov 30 '21 at 16:19

1 Answers1

1
library(dplyr)
mydata %>%
  filter(grepl("^\\s|\\s$", NCT_number))
  NCT_number
1       1111
2       2222
3      4444

Here we are making use of the 'anchors' ^, which denotes the start position of the string, and $, which denotes the end position of the string.

With base R:

mydata[grepl("^\\s|\\s$", mydata$NCT_number),]
[1] " 1111"  "  2222" "4444 "
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34