I would like to replace spaces with linebreaks (\n
) in a pretty long chracter vector in R. However, I don't want to replace every space, but only if the substring exeeds a certain number of characters (n
).
Example:
mystring <- "this string is annoyingly long and therefore I would like to insert linebreaks"
Now I want to insert linebreaks in mystring
at every space on the condition that each substring has a length greater than 20 characters (nchar > 20
).
Hence, the resulting string is supposed to look like this:
"this string is annoyingly\nlong and therefore I would\nlike to insert linebreaks")
Linebreaks (\n
) were inserted after 25, 26 and 25 characters.
How can I achieve this?
Maybe something combining gsub
and strsplit
?