将 R 中的多词句子分成两部分

Split in two pieces a multi-word sentence in R

我需要在 两行 一些多词句子中以编程方式 split/wrap,比如 "This is an example of a multi-word sentence"。如何将最靠近句子中间的白色space替换为"\n"

您也许可以使用 strwrap.

来尝试一些东西

这是一个非常基本的实现:

sentSplit <- function(string, tolerance = 1.05, collapse = "\n") {
  paste(strwrap(string, width = nchar(string)/2 * tolerance), collapse = collapse)
}

sent <- "This is an example of a multi-word sentence"
sentSplit(sent)
# [1] "This is an example of\na multi-word sentence"

cat(sentSplit(sent))
# This is an example of
# a multi-word sentence

"tolerance" 参数基本上是因为在某些情况下,它可能会将字符串分成 3 部分,此时,您可以增加容忍度以最多进行两次分割。

使用strwrap()的简单解决方案:

x <- "This is an example of a multi-word sentence"

wrapInTwo <- function(x, fraction = 0.6) strwrap(x, width = fraction * nchar(x))

wrapInTwo(x)

[1] "This is an example of a" "multi-word sentence"