0

Suppose that I have a matrix:

 a=matrix(1:30,5)

I want to paste the columns 3 by 3 to get this result:

                [,1]                  [,2]
[1,]    "1     6    11"    "16   21   26"
[2,]   " 2     7    12"    "17   22   27"
[3,]   " 3     8    13"    "18   23   28"
[4,]    "4     9    14"    "19   24   29"
[5,]   " 5   10   15"   " 20   25   30"

In fact, I require a function for a very big matrix.

For a simple example we can use whitin and paste (paste several column values into one value in R).

Community
  • 1
  • 1
user1436187
  • 3,252
  • 3
  • 26
  • 59

1 Answers1

2

Try this:

> g <- rep(1:2, each = 3)
> t(apply(a, 1, tapply, g, paste, collapse = " "))
     1         2         
[1,] "1 6 11"  "16 21 26"
[2,] "2 7 12"  "17 22 27"
[3,] "3 8 13"  "18 23 28"
[4,] "4 9 14"  "19 24 29"
[5,] "5 10 15" "20 25 30"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341