insert | R Documentation |
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.
insert(x, row = NULL, col = NULL, repl = NA)
insert_matrix(x, rowsep = NULL, colsep = NULL, rowrep = NA, colrep = rowrep)
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 |
## 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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.