Skip to contents

stm_insert() and its variants insert value(s) into a matrix or vector at a specified location. The function allows inserting data into either rows or columns, or simply appending values to the end.

Usage

stm_insert(dat, insert_at = length(dat), values, axis = NULL)

stm_insert_vector(vec, insert_at = length(vec), values)

stm_insert_rows(mat, insert_at = length(mat), values)

stm_insert_cols(mat, insert_at = length(mat), values)

Arguments

dat

The matrix, vector, or array to which new value(s) will be added.

In stm_insert

Can be a vector or matrix.

In stm_insert_vector

Specifically a vector.

In stm_insert_rows

Specifically a matrix.

In stm_insert_cols

Specifically a matrix.

insert_at

Optional. The location where to insert the new value(s). If length(dat), value(s) are inserted at the end of the matrix or vector.

values

The value(s) to insert as new columns or rows.

axis

Optional. The axis along which to insert the values: 0 for rows, 1 for columns. Default is NULL, meaning the values will be inserted into the vector.

vec

A vector of values.

mat

A matrix of values.

Value

A matrix or vector with the new value(s) inserted at the specified location.

Examples

v <- 11:22
print(v)
#>  [1] 11 12 13 14 15 16 17 18 19 20 21 22
stm_insert(v, insert_at = 3, values = 101:103)
#>  [1]  11  12 101 102 103  13  14  15  16  17  18  19  20  21  22

A <- matrix(dat = v, nrow = 3, byrow = TRUE)
print(A)
#>      [,1] [,2] [,3] [,4]
#> [1,]   11   12   13   14
#> [2,]   15   16   17   18
#> [3,]   19   20   21   22
stm_insert(A, insert_at = 1, values = 101:104, axis = 0)
#>      [,1] [,2] [,3] [,4]
#> [1,]  101  102  103  104
#> [2,]   11   12   13   14
#> [3,]   15   16   17   18
#> [4,]   19   20   21   22

A <- matrix(dat = 11:22, nrow = 3, byrow = TRUE)
print(A)
#>      [,1] [,2] [,3] [,4]
#> [1,]   11   12   13   14
#> [2,]   15   16   17   18
#> [3,]   19   20   21   22
stm_insert(A, insert_at = 3, values = 101:103, axis = 1)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]   11   12  101   13   14
#> [2,]   15   16  102   17   18
#> [3,]   19   20  103   21   22

stm_insert_vector(11:22, insert_at = 1, values = 101:104)
#>  [1] 101 102 103 104  11  12  13  14  15  16  17  18  19  20  21  22
A <- matrix(dat = 11:22, nrow = 3, byrow = TRUE)
stm_insert_rows(A, insert_at = 1, values = 101:104)
#>      [,1] [,2] [,3] [,4]
#> [1,]  101  102  103  104
#> [2,]   11   12   13   14
#> [3,]   15   16   17   18
#> [4,]   19   20   21   22

A <- matrix(dat = 11:22, nrow = 3, byrow = TRUE)
stm_insert_cols(A, insert_at = 3, values = 101:103)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]   11   12  101   13   14
#> [2,]   15   16  102   17   18
#> [3,]   19   20  103   21   22