Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making all instances in a string 5 or less characters

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading