Repeat elements of a vector or matrix
stm_repeat.Rd
stm_repeat()
repeats elements of a vector or matrix. It can repeat
based on the entire vector or column-wise for matrices. Optionally,
it can repeat each element individually.
Arguments
- dat
A vector, matrix, or scalar to be repeated.
- times
A numeric vector or scalar indicating the number of times elements should be repeated.
- is_each
Logical. If
TRUE
, each element ofdat
will be repeated individually. Default isFALSE
.- on_columns
Logical. If
TRUE
, the repetition is done column-wise for matrices. Default isTRUE
.
Details
This function repeats the elements of a vector or matrix. For vectors, elements can be repeated individually. For matrices, repetition can be done either row-wise or column-wise.
Examples
v = c(4, 5, 8, 3)
stm_repeat(v, times = 3)
#> [1] 4 5 8 3 4 5 8 3 4 5 8 3
stm_repeat(v, times = c(3, 2, 1, 5))
#> [1] 4 4 4 5 5 8 3 3 3 3 3
A <- matrix(1:6, nrow = 2, byrow = TRUE)
print(A)
#> [,1] [,2] [,3]
#> [1,] 1 2 3
#> [2,] 4 5 6
stm_repeat(A, times = 3)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
#> [1,] 1 2 3 1 2 3 1 2 3
#> [2,] 4 5 6 4 5 6 4 5 6
stm_repeat(A, times = 3, on_columns = FALSE)
#> [,1] [,2] [,3]
#> [1,] 1 2 3
#> [2,] 4 5 6
#> [3,] 1 2 3
#> [4,] 4 5 6
#> [5,] 1 2 3
#> [6,] 4 5 6