Concatenating N columns of text in R

前端 未结 2 395
我在风中等你
我在风中等你 2021-01-04 22:42

I have an arbitrary number of columns containing text data that have been assembled using the cbind() command, for example:

[1,] \"Text 1,1\" \"Text 1,2\" \"Te

相关标签:
2条回答
  • 2021-01-04 23:27

    Just use paste with its collapse argument:

    R> row <- c("Text 1,1",  "Text 1,2", "Text 1,n")
    R> paste(row, collapse=" ")
    [1] "Text 1,1 Text 1,2 Text 1,n"
    R> 
    

    paste is vectorised, so you can feed it multiple arguments at once.

    0 讨论(0)
  • 2021-01-04 23:33

    It's easy with a data.frame,

    m = matrix(letters[1:12], 3, byrow=TRUE)
    do.call(paste, as.data.frame(m, stringsAsFactors=FALSE))
    #[1] "a b c d" "e f g h" "i j k l"
    
    0 讨论(0)
提交回复
热议问题