I am trying to make all of the words in this data 5 characters or less.
data <- "The prisoner and his friend escaped but were later caught"
Everything that I have tried is only allowing me to take the same number of characters off of each term, but I am wanting to only take enough off of each to
The result I am looking for is:
The priso and his frien escap but were later caugh
>Solution :
Try
library(stringr)
str_replace_all(data, "\\b(\\S{5})\\S+\\b", "\\1")
[1] "The priso and his frien escap but were later caugh"