将字符向量折叠为 R 中的单个观察值

Collapse character vector into single observation in R

如何将多值向量简化为单个观察值?具体来说,处理文本。该解决方案应该是可扩展的。

考虑:

col <- c("This is row 1", "AND THIS IS ROW 2", "Wow, and this is row 3!")

其中returns以下:

> col
[1] "This is row 1"           "AND THIS IS ROW 2"       "Wow, and this is row 3!"

所需的解决方案如下所示:

> col
[1] "This is row 1 AND THIS IS ROW 2 Wow, and this is row 3!"

您正在寻找?paste

> paste(col, collapse = " ")
#[1] "This is row 1 AND THIS IS ROW 2 Wow, and this is row 3!"

在这种情况下,您希望将字符串折叠在一起并在它们之间添加一个 space。您还可以查看 paste0.