Given two matrices:
test1 <- matrix(1:5,ncol=1)
test2 <- matrix(6:10,ncol=1)
I would like to combine them into one single matrix row by row in an alternating way, meaning:
expected_output <- matrix(c(1,6,2,7,3,8,4,9,5,10),ncol=1)
or more visual:
I already created an empty matrix double the length of test1, but i am lacking how to add row by row each value. rbind()
is only adding the whole matrix.
c(mapply(append, test1, test2))
[1] 1 6 2 7 3 8 4 9 5 10
if matrix outut is needed, use
as.matrix(c(mapply(append, test1, test2)), ncol = 1)
[,1]
[1,] 1
[2,] 6
[3,] 2
[4,] 7
[5,] 3
[6,] 8
[7,] 4
[8,] 9
[9,] 5
[10,] 10