rdevn00b
rdevn00b

Reputation: 562

Grep not working in R

I am having trouble understanding why my grep function in R is not working. I think this has something to do with the data type of my check word and my keyword list. Here is my output:

> check
[1] "DESIRE"
> delwords
          delete
1        INITIAL
2    DELIVERABL
3    DEMONSTRAT
4         DESIR
5        DESIRE
> grep(check,delwords)
integer(0)
> typeof(check)
[1] "character"
> typeof(delwords)
[1] "list"

Upvotes: 0

Views: 2524

Answers (2)

RHertel
RHertel

Reputation: 23788

As pointed out be @grubjesic, the form of output that you posted of delwords suggests that

> class(delwords)
[1] "data.frame"

If this is the case, then the solution presented in a comment by @akrun works:

> grep(check,delwords$delete)
[1] 5

Alternatively, if you don't necessarily need to use grep, you can obtain the same result with

> which(delwords$delete==check)
[1] 5

And, as a final test, you may try:

> delwords[grep(check,delwords$delete),]
[1] DESIRE
Levels: DELIVERABL DEMONSTRAT DESIR DESIRE INITIAL

In this post I only expanded on the suggestions by @akrun and @grubjesic. They deserve the credits. The sole purpose of this post is to provide some further clarifications.

Upvotes: 1

milos.ai
milos.ai

Reputation: 3930

Seems to me delwords here is actually data.frame. Please try to see what is class of delwords. It should be list, not data.frame

class(delwords)

Also, I have created this sample :

check  = "DESIRE"
delwords = list("INITIAL","DELIVERABL","DEMONSTRAT","DESIR","DESIRE")

grep(check,delwords)

and it seems it is working correctly:

[1] 5

Upvotes: 2

Related Questions