Mimics some of the behavior of the Riffle function (http://reference.wolfram.com/mathematica/ref/Riffle.html) in Mathematica. For matrices, it interleaves the columns. For vectors, it interleaves differently according to whether the subsequent values are presented as separate values or whether they are grouped with c().

Riffle(...)

Arguments

...

The objects or values that need to be interleaved.

Value

A vector or a matrix depending on the input. If one or more input objects is a matrix, the result will also be a matrix.

Details

It is expected that all matrices to be interleaved would have the same number of rows, though they may have differing numbers of columns. If they have differing numbers of columns, they are all made to conform to the same dimension before proceeding by recycling the existing columns.

References

http://stackoverflow.com/q/21347207/1270695

Author

Ananda Mahto

Examples

m1 <- matrix(1:9, nrow = 3, ncol = 3) m2 <- matrix(letters[1:9], nrow = 3, ncol = 3) Riffle(m1, m2)
#> [,1] [,2] [,3] [,4] [,5] [,6] #> [1,] "1" "a" "4" "d" "7" "g" #> [2,] "2" "b" "5" "e" "8" "h" #> [3,] "3" "c" "6" "f" "9" "i"
Riffle(m1, "||", m2)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] #> [1,] "1" "||" "a" "4" "||" "d" "7" "||" "g" #> [2,] "2" "||" "b" "5" "||" "e" "8" "||" "h" #> [3,] "3" "||" "c" "6" "||" "f" "9" "||" "i"
m3 <- matrix(LETTERS[1:6], nrow = 3, ncol = 2) Riffle(m1, m2, m3)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] #> [1,] "1" "a" "A" "4" "d" "D" "7" "g" "A" #> [2,] "2" "b" "B" "5" "e" "E" "8" "h" "B" #> [3,] "3" "c" "C" "6" "f" "F" "9" "i" "C"
## Just vectors Riffle(1:6, "x")
#> [1] "1" "x" "2" "x" "3" "x" "4" "x" "5" "x" "6" "x"
Riffle(1:6, "x", "y")
#> [1] "1" "x" "y" "2" "x" "y" "3" "x" "y" "4" "x" "y" "5" "x" "y" "6" "x" "y"
Riffle(1:6, c("x", "y"))
#> [1] "1" "x" "2" "y" "3" "x" "4" "y" "5" "x" "6" "y"