[R] pasting elements of one character vector together

Sarah Goslee sarah.goslee at gmail.com
Tue Sep 20 12:19:18 CEST 2011


It isn't entirely clear to me what you want, but here are all the possibilities
I could think of. I hope one of them does what you want.

> testmat <- matrix(1:8, ncol=2)
> testmat
     [,1] [,2]
[1,]    1    5
[2,]    2    6
[3,]    3    7
[4,]    4    8
> paste(testmat, collapse=" ")
[1] "1 2 3 4 5 6 7 8"
> paste(t(testmat), collapse=" ")
[1] "1 5 2 6 3 7 4 8"
> apply(testmat, 1, paste, collapse=" ")
[1] "1 5" "2 6" "3 7" "4 8"
> apply(testmat, 2, paste, collapse=" ")
[1] "1 2 3 4" "5 6 7 8"

Sarah

On Tue, Sep 20, 2011 at 5:55 AM, Marion Wenty <marion.wenty at gmail.com> wrote:
> I have another question concerning the paste command:
>
> now instead of a vector I would like to paste the elements of a matrix
> together, which works in the same:
>
> Mypastedmatrix <- paste(Mymatrix,collapse="")
>
> My problem now is that the program does this BY COLUMN, but I would like to
> have the elements pasted together BY ROW.
>
> Could anybody help me with this?
>
> Marion
>
-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list