title: tm_map(gsub...) fails to replace words
body: <pre><code># Loading required libraries
  
  
  # Set up logistics such as reading in data and setting up corpus
  
  ```{r}
  
  # Relative path points to the local folder
  folder.path="../data/InauguralSpeeches/"
  
  # get the list of file names
  speeches=list.files(path = folder.path, pattern = "*.txt")
  
  # Truncate file names so it is only showing "FirstLast-Term"
  prez.out=substr(speeches, 6, nchar(speeches)-4)
  
  # Create a vector NA's equal to the length of the number of speeches
  length.speeches=rep(NA, length(speeches))
  
  # Create a corpus
  ff.all&lt;-Corpus(DirSource(folder.path))
  ```
  
  # Clean the data
  
  ```{r}
  
  # Use tm_map to strip all white spaces to a single space, to lower case case, remove stop words, empty strings and punctuation.
  ff.all&lt;-tm_map(ff.all, stripWhitespace)
  ff.all&lt;-tm_map(ff.all, content_transformer(tolower))
  ff.all&lt;-tm_map(ff.all, removeWords, stopwords("english"))
  ff.all&lt;-tm_map(ff.all, removeWords, c("can", "may", "upon", "shall", "will",     "must", ""))
  </code></pre>
  
  <h1>The problem line</h1>
  
  <p>ff.all&lt;-tm_map(ff.all, gsub, pattern = "free", replacement = "freedom")</p>
  
  <pre><code>ff.all&lt;-tm_map(ff.all, removeWords, character(0))
  ff.all&lt;-tm_map(ff.all, removePunctuation)
  
  # tdm.all =  a Term Document Matrix
  tdm.all&lt;-TermDocumentMatrix(ff.all)
  </code></pre>
  
  <p>So I am trying to replace words that are similar by one root word. For example, replacing "free" by "freedom" in a text mining project.</p>
  
  <p>Then I learned this line from a Youtube tutorial: ff.all&lt;-tm_map(ff.all, gsub, pattern = "free", replacement = "freedom").
  Without this line, the code runs.</p>
  
  <p>With this line added, R Studio gives this error "<strong>Error: inherits(doc, "TextDocument") is not TRUE</strong>" on the execution of this line: "<em>tdm.all&lt;-TermDocumentMatrix(ff.all)</em>"</p>
  
  <p>I think this should be a relatively simple issue, however I could not find a solution on stackoverflow. </p>