【发布时间】:2016-12-12 13:52:25
【问题描述】:
我想在 R 中相当长的字符向量中用换行符 (\n) 替换空格。但是,我不想替换 每个 空格,但前提是子字符串执行完毕一定数量的字符 (n)。
例子:
mystring <- "this string is annoyingly long and therefore I would like to insert linebreaks"
现在我想在每个空格的mystring 中插入换行符,条件是每个子字符串的长度大于 20 个字符 (nchar > 20)。
因此,生成的字符串应该如下所示:
"this string is annoyingly\nlong and therefore I would\nlike to insert linebreaks")
在 25、26 和 25 个字符之后插入换行符 (\n)。
我怎样才能做到这一点?
也许结合了gsub 和strsplit?
【问题讨论】: