0

I would like to obtain the indices of all elements of a character vector that contain " + ".

  • c("A + B", "C") should output 1.
  • c("A + B", "C + D") should output c(1, 2).
  • c("A", "B") should output integer(0).

I have tried using grep(pattern = " + ", x), but this returns integer(0) regardless of what x is. I have also tried using grep(pattern = "+", x), but this returns seq_along(x) no matter what x is. I suspect that this has to do with + being a special character, but I'm not sure how to fix this.

dext
  • 1
  • 3

2 Answers2

1

+ is a special character in regex, you need to escape it with \\.

We can use grep to get index of pattern match.

grep('\\+', c("A + B", "C"))
#[1] 1
grep('\\+', c("A + B", "C + D"))
#[1] 1 2
grep('\\+', c("A", "B")) 
#integer(0)

We can also use grepl which returns boolean values, then wrap it in which to get index.

grepl('\\+', c("A + B", "C"))
#[1]  TRUE FALSE

which(grepl('\\+', c("A + B", "C")))
#[1] 1
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

Maybe what you need to do is just turning on the option fixed to TRUE in grep, i.e.,

> grep('+', c("A + B", "C"), fixed = TRUE)
[1] 1
> 
> grep('+', c("A + B", "C + D"), fixed = TRUE)
[1] 1 2
> 
> grep('+', c("A", "B"), fixed = TRUE)
integer(0)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81