【发布时间】:2020-08-29 13:35:56
【问题描述】:
我想创建一个 R 函数,在字符串中的每 n 个单词之后插入一个“\n”(其中 n 是一个参数)。
例如
startstring <- "I like to eat fried potatoes with gravy for dinner."
myfunction(startstring, 4)
会给:
"I like to eat\nfried potatoes with gravy\nfor dinner."
我相信要做到这一点,我需要将字符串分成几个部分,每个部分长 n 个单词,然后用分隔符“\n”将它们粘贴在一起。但是我不知道如何进行初始拆分步骤。
谁能给点建议?
【问题讨论】:
-
看看 Base R 中的
strsplit()。