Skip to contents

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.

Usage

stm_repeat(dat, times, is_each = FALSE, on_columns = TRUE)

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 of dat will be repeated individually. Default is FALSE.

on_columns

Logical. If TRUE, the repetition is done column-wise for matrices. Default is TRUE.

Value

A vector or matrix with repeated elements.

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