
Last chance! 50% off unlimited learning
Sale ends in
This function inserts one or more columns or rows before or after another column or row in a data frame or matrix. It is similar to cbind
except that the inserted column(s)/row(s) can be placed anywhere.
insertCol(x, into, at = NULL, before = TRUE)insertRow(x, into, at = NULL, before = TRUE)
A data frame.
Data frame, matrix, or vector with same number of columns or rows or elements as into
.
Data frame or matrix into which x
is to be inserted.
Character, integer, or NULL
. Name of column or column number or name of row or row number at which to do insertion. If NULL
(default), the result is exactly the same as cbind(into, x
except that it retains row numbers or column names from into
.
Logical, if TRUE
(default) then the insertion will occur in front of the column or row named in at
, if FALSE
then after. Ignored if at
is NULL
.
insertRow()
: Insert a column or row into a data frame or matrix
x <- data.frame(y1=11:15, y2=rev(letters)[1:5])
into <- data.frame(x1=1:5, x2='valid', x3=letters[1:5], x4=LETTERS[1:5], x5='stuff')
insertCol(x, into=into, at='x3')
insertCol(x, into=into, at='x3', before=FALSE)
insertCol(x, into)
x <- data.frame(x1=1:3, x2=LETTERS[1:3])
into <- data.frame(x1=11:15, x2='valid')
row.names(into) <- letters[1:5]
insertRow(x, into=into, at='b')
insertRow(x, into=into, at='b', before=FALSE)
insertRow(x, into)
Run the code above in your browser using DataLab