0

Here is the code I am working with:

A <- matrix(1:9, nrow = 3)
A
cbind(A,A,A)

This gives an output:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    4    7    1    4    7    1    4    7
[2,]    2    5    8    2    5    8    2    5    8
[3,]    3    6    9    3    6    9    3    6    9

The desired output is...

  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    1    1    4    4    4    7    7    7
[2,]    2    2    2    5    5    5    8    8    8
[3,]    3    3    3    6    6    6    9    9    9

In addition I tried this...

test <- (sapply(A , function(maybe) rep(maybe,each=3)))
test

Which gives an output of:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    2    3    4    5    6    7    8    9
[2,]    1    2    3    4    5    6    7    8    9
[3,]    1    2    3    4    5    6    7    8    9

The help is much appreciated.

user227710
  • 3,164
  • 18
  • 35
Adam Warner
  • 1,334
  • 2
  • 14
  • 30
  • are you trying to get this format on the first attempt or do you have `A` and need to interleave the columns x number of times? – rawr Jul 10 '15 at 21:55
  • i don't need to get the format on the first go, I mean of course it would be nice. But yes I am trying to get it on the first attempt. – Adam Warner Jul 10 '15 at 22:02

1 Answers1

5

Use rep with column indexing: A[,rep(1:3, each=3)]

Neal Fultz
  • 9,282
  • 1
  • 39
  • 60