How to search if there is a word inside a text in R

I need to know if inside a certain text has a specific word, for example fnord. So far I only found:

x<-"fnord: Você não tem nivel de acesso a está informação."
strsplit(x, "[[:punct:] ]")
[[1]]
[1] "fnord"      ""           "Você"       "não"        "tem"        "nivel"      "de"        
[8] "acesso"     "a"          "está"       "informação"

I saw something about the function grep, but I couldn't get it right.
and I would prefer an answer of type TRUE or FALSE.

 3
Author: Carlos Eduardo Lagosta, 2018-07-24

1 answers

@ rui-barradas has already given the answer in the comments, this is only an elaboration. First, let's create an example with more sentences.

string.vector <- c(
  "gnoll: Você tem acesso total a esta informação.",
  "stack: Você tem acesso parcial a esta informação.",
  "fnord: Você não tem nivel de acesso a esta informação.",
  "fnord: Você poderia ter acesso a esta informação."
)

> string.vector
[1] "gnoll: Você tem acesso total a esta informação."       
[2] "stack: Você tem acesso parcial a esta informação."     
[3] "fnord: Você não tem nivel de acesso a esta informação."
[4] "fnord: Você poderia ter acesso a esta informação."     

grep it will give you the indices (positions) in which the pattern you want was found. grepl will give you True / False for all positions:

> grep('fnord', string.vector)
[1] 3 4

> grepl('fnord', string.vector)
[1] FALSE FALSE  TRUE  TRUE

Strsplit generates a list with the strings separated according to the criteria you set (scores and spaces, in this case):

split.list <- strsplit(string.vector, "[[:punct:] ]")

> split.list
[[1]]
[1] "gnoll"      ""           "Você"       "tem"        "acesso"    
[6] "total"      "a"          "esta"       "informação"

[[2]]
[1] "stack"      ""           "Você"       "tem"        "acesso"    
[6] "parcial"    "a"          "esta"       "informação"

[[3]]
[1] "fnord"      ""           "Você"       "não"        "tem"       
[6] "nivel"      "de"         "acesso"     "a"          "esta"      
[11] "informação"

[[4]]
[1] "fnord"      ""           "Você"       "poderia"    "ter"       
[6] "acesso"     "a"          "esta"       "informação"

Applying grep and grepl here will give you the same result, because it is being indicated which items in the list have the search key:

> grep('fnord', split.list)
[1] 3 4

> grepl('fnord', split.list)
[1] FALSE FALSE  TRUE  TRUE

You can use lapply to apply grep / grepl to each element of the list

> lapply(split.list, grep, pattern = 'fnord')
[[1]]
integer(0)

[[2]]
integer(0)

[[3]]
[1] 1

[[4]]
[1] 1

> lapply(split.list, grepl, pattern = 'fnord')
[[1]]
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

[[2]]
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

[[3]]
[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

[[4]]
[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

The stringr package has user-friendly functions for working with text strings. Check his vignette over regular expressions .

 4
Author: Carlos Eduardo Lagosta, 2018-07-25 01:52:09