我有一些包含文本数据的任意列,这些列是使用cbind()命令组装的,例如:
[1,] "Text 1,1" "Text 1,2" "Text 1,n"
[2,] "Text 2,1" "Text 2,2" "Text 2,n"
[3,] "Text 3,1" "Text 3,2" "Text 3,n"
[n,] "Text n,1" "Text n,2" "Text n,n"
我想将每行连接在一起,得到:
[1,] "Text 1,1 Text 1,2 Text 1,n"
[n,] "Text n,1 Text n,2 Text n,n"
目前,我使用一个for循环来完成这个过程(其中textColumns是cbind()矩阵):
concatColumn <- c()
for (i in 1:ncol(textColumns)) concatColumn <- paste(concatColumn,textColumns[,i])
在R中是否有更简单、更优雅的方法来完成这个任务?我一直在寻找使用paste()命令而不需要for循环的方法,但是一直没有找到解决方案。非常感谢您的帮助!
apply(m, 1, paste, collapse=" ")
直接处理矩阵。 - baptiste[1] "c(\"t1\", \"t2\", \"t3\")" "c(\"t4\", \"t5\", \"t6\")" "c(\"t7\", \"t8\", \"t9\")"
- Timothy P. Jurka