Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well let's work through the issues then...</p> <p>I'm guessing you have a data.frame that looks like this:</p> <pre><code> person sex adult state code 1 sam m 0 Computer is fun. Not too fun. K1 2 greg m 0 No it's not, it's dumb. K2 3 teacher m 1 What should we do? K3 4 sam m 0 You liar, it stinks! K4 5 greg m 0 I am telling the truth! K5 6 sally f 0 How can we be certain? K6 7 greg m 0 There is no way. K7 8 sam m 0 I distrust you. K8 9 sally f 0 What are you talking about? K9 10 researcher f 1 Shall we move on? Good then. K10 11 greg m 0 I'm hungry. Let's eat. You already? K11 </code></pre> <p>This data set comes from the qdap package. to get qdap use <code>install.packages("qdap")</code>.</p> <p>Now to make the reproducible example I was talking about with your data set do what I'm doing here with the <code>DATA</code> data set from qdap.</p> <pre><code>DATA dput(head(DATA)) </code></pre> <p>Ok now for your original problem I think <code>wfm</code> will do what you want:</p> <pre><code>freqs &lt;- t(wfm(DATA$state, 1:nrow(DATA))) data.frame(DATA, freqs, check.names = FALSE) </code></pre> <p>If you only wanted the top so many words use an ordering technique like I use here:</p> <pre><code>freqs &lt;- t(wfm(DATA$state, 1:nrow(DATA))) ords &lt;- rev(sort(colSums(freqs)))[1:9] #top 9 words top9 &lt;- freqs[, names(ords)] #grab those columns from freqs data.frame(DATA, top9, check.names = FALSE) #put it together </code></pre> <p>The outcome looks like this:</p> <pre><code>&gt; data.frame(DATA, top9, check.names = FALSE) person sex adult state code you we what not no it's is i fun 1 sam m 0 Computer is fun. Not too fun. K1 0 0 0 1 0 0 1 0 2 2 greg m 0 No it's not, it's dumb. K2 0 0 0 1 1 2 0 0 0 3 teacher m 1 What should we do? K3 0 1 1 0 0 0 0 0 0 4 sam m 0 You liar, it stinks! K4 1 0 0 0 0 0 0 0 0 5 greg m 0 I am telling the truth! K5 0 0 0 0 0 0 0 1 0 6 sally f 0 How can we be certain? K6 0 1 0 0 0 0 0 0 0 7 greg m 0 There is no way. K7 0 0 0 0 1 0 1 0 0 8 sam m 0 I distrust you. K8 1 0 0 0 0 0 0 1 0 9 sally f 0 What are you talking about? K9 1 0 1 0 0 0 0 0 0 10 researcher f 1 Shall we move on? Good then. K10 0 1 0 0 0 0 0 0 0 11 greg m 0 I'm hungry. Let's eat. You already? K11 1 0 0 0 0 0 0 0 0 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload