I have a column of firm names in an R dataframe that goes something like this:
"ABC Industries"
"ABC Enterprises"
"123 and 456 Corporation"
"XYZ Company"
And so on. I'm trying to generate frequency tables of every word that appears in this column, so for example, something like this:
Industries 10
Corporation 31
Enterprise 40
ABC 30
XYZ 40
I'm relatively new to R, so I was wondering of a good way to approach this. Should I be splitting the strings and placing every distinct word into a new column? Is there a way to split up a multi-word row into multiple rows with one word?
You can use the package
tidytext
anddplyr
:If you wanted to, you could do it in a one-liner:
Here I use
strsplit()
to break each entry intro components; this returns a list (within a list). I usedo.call()
so simply concatenate all result lists into one vector, whichtable()
summarises.Here is another one-liner. It uses
paste()
to combine all of the column entries into a single long text string, which it then splits apart and tabulates: