1

grep always output [1]1, but not the real index

a = "d123  d123 d123 asdf asd D123"
grep("d", a)
[1] 1

There are several "d" in the variable a, but not all the indices are displayed. I tried http://rfunction.com/archives/1481 and it works.

Why the simple one does not?

It is supposed to be equivalent to

unlist(gregexpr("d",a)[1])
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Justin
  • 327
  • 3
  • 13
  • Judging by the answers, you should clarify what is meant by "index". In your linked example, it's the index of a vector, hence my answer. But you mean the position in the string, in which case Ronak's answer. – neilfws May 08 '19 at 03:45

2 Answers2

3

EDIT: I assumed from your linked example that index referred to a vector. But now that I run the second code example in your question, I see you want index = position in string. So @ronak-shah is correct, not me.

I think you want:

a = c("d123",  "d123", "d123", "asdf", "asd", "D123")

since your current variable a is one string with length = 1.

neilfws
  • 32,751
  • 5
  • 50
  • 63
2

From ?grep

grep(value = FALSE) returns a vector of the indices of the elements of x that yielded a match

For example see,

x <- c("abc", "ddddd", "ads", "ccc")

grep("d", x)
#[1] 2 3

This means that x[2] and x[3] have d in them irrespective of the number of ds in them.

Since you want position of ds in the string grep isn't the right choice here. You could do

which(strsplit(a, "")[[1]]=="d")
#[1]  1  7 12 19 24

and with grep it would be

grep("d", strsplit(a, "")[[1]])
#[1]  1  7 12 19 24

which would give you equivalent of unlist(gregexpr("d",a)[1]).

Or use any of the method given in this link.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213