Count Word Frequency in Character String in R (Example Code)

In this tutorial, I’ll demonstrate how to get the word frequencies in a text element in R.

Creating Example Data

my_string <- "this is a string this string is nice"               # Create character string
my_string                                                         # Display character string
# [1] "this is a string this string is nice"

Example: Get Frequency of Words in Character String

sort(table(unlist(strsplit(my_string, " "))), decreasing = TRUE)  # Get frequencies
#     is string   this      a   nice 
#      2      2      2      1      1

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top