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" |
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 |
sort(table(unlist(strsplit(my_string, " "))), decreasing = TRUE) # Get frequencies # is string this a nice # 2 2 2 1 1