This is the text for the famous “sea shells” tongue twister:
sea_shells <- c("She", "sells", "sea", "shells", "by", "the", "seashore",
"The", "shells", "she", "sells", "are", "surely", "seashells",
"So", "if", "she", "sells", "shells", "on", "the", "seashore",
"I'm", "sure", "she", "sells", "seashore", "shells")
Use the nchar() function to calculate the number of characters in each word (you can call this object shell_chars). Now loop over the possible word lengths, collecting words with the same length in one component of the list char_lengths. In other words, the first component of char_lengths should be the vector c("by", "So", "if", "on"), the second component of char_lengths should be the vector c("She", "sea", "the", "The", "she", "are", "I'm") and so on. Note that this means collecting unique items.
(from: Cotton, 2013, Learning R, O’Reilly)