0

I have a list of words, each word appears in a text (there are 5 texts) and has a frequency in each text. I want an output which can group the word and the text(s) that it appears and the frequency score(s). For example:

df <- data.frame(word=c("a", "b", "c", "e", "g", "a", "c", "f", "b", "d", "e", "c", "a", "h", "i"),text=c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5), freq=c(10,3,5,8,2,6,7,4,9,11,18,2,1,5,6))
> print(df)
   word text freq
1     a     1   10
2     b     1    3
3     c     1    5
4     e     2    8
5     g     2    2
6     a     2    6
7     c     3    7
8     f     3    4
9     b     3    9
10    d     4   11
11    e     4   18
12    c     4    2
13    a     5    1
14    h     5    5
15    i     5    6 

I want to have an output that shows , for example, word "a" appearing in text 1, 2, and 5 with the frequency being 10, 6, and 1 respectively. Similarly to all the other words in the list.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • This one wasn't given the correct solution in the Question Close. The OP already has the frequency column. – stefan_aus_hannover Jun 21 '23 at 20:58
  • I'm confused. *"I want to have an output that shows , for example, word "a" appearing in text 1, 2, and 5 with the frequency being 10, 6, and 1 respectively"* is exactly what I see in your sample data. Rows 1, 6, and 13 show word "a" appearing in text 1, 2, and 5, with frequencies 10, 6, and 1. Could you show your desired output, since to me it looks like your input is the desired output? – Gregor Thomas Jun 21 '23 at 20:59
  • @GregorThomas the want their input sorted by `word` and then `text`. They already have the `freq` column. – stefan_aus_hannover Jun 21 '23 at 21:00
  • Perhaps you want a different, wider format for this table? Or perhaps you want separate subsets of this table for each word? Or perhaps as stefan suggests you like your table's format but you want to re-order the rows? – Gregor Thomas Jun 21 '23 at 21:00
  • Perhaps, or an `arrange` solution, or a `split` solution. After my first assumption is wrong, I'm slower on my next one. – Gregor Thomas Jun 21 '23 at 21:02
  • yes I want to have a subset of table for each word. Like word "a" appears in which texts, and the corresponding frequency in each text. – user21390049 Jun 21 '23 at 21:02
  • 1
    Sounds like `my_tables = split(df, df$word)`. That will give you a `list` where each item is a subset of your big table. You can access the individual tables e.g., with `my_tables$a` or `my_tables[["a"]]`, or just `my_tables` to see them all. – Gregor Thomas Jun 21 '23 at 21:10
  • Or `arrange(df,word,text)` – stefan_aus_hannover Jun 21 '23 at 21:12
  • 1
    Many thanks @GregorThomas and @stefan_aus_hannover!! they all work for me now!! – user21390049 Jun 21 '23 at 21:16

0 Answers0