title: Fundamental problem - Word frequencies/histogram in R
body: <p>I've spent an entire day on this and I cannot seem to find my mistake. I'm sure someone else will be able to spot it in a second (for which I am thankful).</p>
  <p>I have a few text files that I need to analyze; it doesn't matter what's in them (use lorem ipsum for a reproducible example). I am at the EDA step. My code looks like this:</p>
  <pre><code>lorem &lt;- readLines(&quot;lorem.txt&quot;, skipNul = TRUE) # Read in data; this works
  
  # stringi functions
  library(stringi)
  
  # Count words in each entry; this also works fine
  lorem_wc &lt;- data.frame(stri_count_words(lorem))
  </code></pre>
  <p>This produces a data frame with one column with the number of words in each line - so far, this works great. Now I'd like to create a histogram (i.e., see a distribution of word-count frequencies); the number of words would be on the horizontal axis and the frequency of each would be on the vertical. My thought was this:</p>
  <pre><code>lorem_df &lt;- table(lorem_wc)
  </code></pre>
  <p>This produces something I don't want (I'm not actually sure what this is, but it's clearly not what I want - it's a single row of information, but I don't understand what R computed. If I try this:</p>
  <pre><code>lorem_df &lt;- data.frame(table(stri_count_words(lorem)))
  </code></pre>
  <p>I get the same information, but it's now in a column instead of a row. The contents are still not what I'd expect. So I created a test file and did the same thing:</p>
  <pre><code>testfile &lt;- readLines(&quot;test.txt&quot;, skipNul = TRUE)
  testtab &lt;- data.frame(table(stri_count_words(testfile)))
  </code></pre>
  <p>When I print <code>testtab</code>, it looks like a two-column data frame; the first column is called <code>Var1</code> and looks like it's the number of words per line in the text file called &quot;test.txt&quot;. The second column is called <code>Freq</code> and looks like it's the frequency that goes with the number of words in the first column.</p>
  <p>Now I use ggplot to generate a histogram:</p>
  <pre><code>g &lt;- ggplot(testtab, aes(x = Var1))
  g &lt;- g + geom_histogram()
  g
  </code></pre>
  <p>... which throws an error: <code>Error: StatBin requires a continuous x variable: the x variable is discrete. Perhaps you want stat=&quot;count&quot;?</code></p>
  <p>I'm sure this is trivial, but I've apparently gone into vapor lock.</p>
  <p>Thanks in advance.</p>