Is there a way to combine three arrays in R so that the first row of the first array is followed by the first row of the second array and that is followed by the third row of the third array? So, if I ran the following code:
> number1<-rbind(rep("A",3), rep("B",3), rep("C",3))
> number1
[,1] [,2] [,3]
[1,] "A" "A" "A"
[2,] "B" "B" "B"
[3,] "C" "C" "C"
> number2<-rbind(rep(1,3), rep(2,3), rep(3,3))
> number2
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 3 3
> number3<-rbind(rep("X",3), rep("Y",3), rep("Z",3))
> number3
[,1] [,2] [,3]
[1,] "X" "X" "X"
[2,] "Y" "Y" "Y"
[3,] "Z" "Z" "Z"
The result would look like this:
[,1] [,2] [,3]
[1,] "A" "A" "A"
[2,] "1" "1" "1"
[3,] "X" "X" "X"
[4,] "B" "B" "B"
[5,] "2" "2" "2"
[6,] "Y" "Y" "Y"
[7,] "C" "C" "C"
[8,] "3" "3" "3"
[9,] "Z" "Z" "Z"
I have tried melt but I can't get it to work.
You could try this:
> matrix(t(cbind(number1,number2,number3)),ncol=3, byrow=T)
# [,1] [,2] [,3]
# [1,] "A" "A" "A"
# [2,] "1" "1" "1"
# [3,] "X" "X" "X"
# [4,] "B" "B" "B"
# [5,] "2" "2" "2"
# [6,] "Y" "Y" "Y"
# [7,] "C" "C" "C"
# [8,] "3" "3" "3"
# [9,] "Z" "Z" "Z"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With