insert: Insert

View source: R/utils.R

insertR Documentation

Insert

Description

Insert rows and/or columns into a matrix or elements into a vector at specified indices. insert and insert_matrix are similar functions with the latter defined to maintain existing code.

Usage

insert(x, row = NULL, col = NULL, repl = NA)

insert_matrix(x, rowsep = NULL, colsep = NULL, rowrep = NA, colrep = rowrep)

Arguments

x

a matrix

row, col

index of row or column to shift right or down, respectively

repl

replacement values, recycled if needed

rowsep, colsep

index of row or column to shift right or down, respectively

rowrep, colrep

row and column replacement values, recycled if needed; note that rows are replaced first followed by columns, so for the opposite behavior, transpose m before and after using insert_matrix and swap rowrep and colrep; see examples

Examples

## insert at index for vectors
insert(1:5, 4)
insert(1:5, 4, repl = 10)

m <- col(matrix(0, 5, 5))
insert_matrix(m, 2, c(2,4,4,4,6))

## anticipate number of values needed for replacement(s)
# insert_matrix(m, 4, 4:5, colrep = 1:ncol(m)) ## error
insert_matrix(m, 4, 4:5, colrep = 1:6)

## these are _almost_ identical -- rows are inserted first
insert_matrix(m, 5, 5, 0, 1) == t(insert_matrix(t(m), 5, 5, 1, 0))


raredd/rawr documentation built on March 4, 2024, 1:36 a.m.