0

I am looking to learn R, and after a few google searches was able to create following code to find multiply occurrence of key words in a text file.

I have the following code to find multiply occurrences of key word expansion

url <- "http://www.textfiles.com/science/blackhol.txt"
file <- download.file(url = url, destfile ="blackhole.txt")

textfile <- "~/blackhole.txt"

text <-readLines(textfile)

library(qdap)

d <- sent_detect(text)



# grep the sentence with the keyword:
n <- which(grepl('[Ee]pansion', d) == T)

# Obtaining 2 sentences before keyword and 2 sentences after keyword:
d[(n - 2):(n + 2)]

However, this does not work as I obtain an error:

Error in (n - 2):(n + 2) : argument of length 0

How do I resolve this issue, and thereafter use following code below to provide output for all multiply occurrences of keyword expansion:

lapply(which(grepl('expansion', d) == T), function(n){cat(d[(n - 1):(n + 1)])
})

Thank you for your help.

Beginner
  • 262
  • 1
  • 4
  • 12
  • You have a typo in your first `grepl` function it should be **[Ee]xpansion** I suspect the error is because you are not finding anything from the grep. – emilliman5 Mar 02 '17 at 13:07
  • Thank you emilliman5. I get a new error: `Warning messages: 1: In (n - 1):(n + 1) : numerical expression has 4 elements: only the first used`. Please help – Beginner Mar 02 '17 at 14:06
  • `lapply(n, function(x) d[(x-2):(x+2)]`, FYI: this will throw an error if `n` in the first or last sentence, because of an index out of bounds or negative subscript error. – emilliman5 Mar 02 '17 at 14:52
  • Works great @ emilliman5. Thank you. How do I account for this out of bounds error?Also if I want to add more search words, do I add as `c([Ee]xpansion, [Bb]oulder])`? – Beginner Mar 02 '17 at 16:08
  • There are numerous [answers](http://stackoverflow.com/questions/7597559/grep-using-a-character-vector-with-multiple-patterns) on using multiple patterns in grep, so I will leave that for you to research. For the edge cases you will need to add some sort of check in the `lapply` loop. along the lines of `if(x-2<0){...}`. – emilliman5 Mar 02 '17 at 16:50

0 Answers0