Add Rows to Data Objects `rbind<-` adds rows to data objects as a side effect. The purpose of the function is to replace the need to use dat2 <- rbind(dat1, add1); dat3 <- rbind(dat2, add2); dat4 <- rbind(dat3, add3), etc. For data.frames, it functions similarly to `[<-.data.frame`, but allows you to specify the location of the rows similar to append (vs. c) and overwrite rows with the same rownames. For matrices, it offers more novel functionality since `[<-.matrix` does not exist.
Some traditional R folks may find this function uncomfortable. R is famous for limiting
side effects, except for a few notable exceptions (e.g., `[<-`
and `names<-`
).
Part of the reason is that side effects can be computationally inefficient in R.
The entire object often has to be re-constructed and re-saved to memory. For
example, a more computationally efficient alternative to rbind(dat) <- add1;
rbind(dat) <- add2; rbind(dat) <- add3 is dat1 <- do.call(what = rbind,
args = list(dat, add1, add2, add3)). However, `rbind<-`
was not created
for R programming use when computational efficiency is valued; it is created
for R interactive use when user convenience is valued.
rbind(data, after = nrow(data), row.nm = NULL, overwrite = TRUE) <- value
data |
data.frame or matrix of data. |
after |
either an integer vector with length 1 or a character vector of
length 1 specifying where to add |
row.nm |
character vector of length equal to |
overwrite |
logical vector of length 1 specifying whether rows from
|
value |
data.frame, matrix, or atomic vector to be added as rows to
|
Similar to `rbind`
, `rbind<-`
works with both data.frames and matrices.
This is because `rbind`
is a generic function with a default method that
works with matrices and a data.frame method that works with data.frames. Similar
to `rbind`
, if rownames of value
are not given and row.nm
is left NULL, then the rownames of the return object are automatically created
and can be dissatisfying.
Like other similar functions (e.g., `names<-`
and `[<-`
),
`rbind<-`
does not appear to have a return object. However, it technically
does as a side effect. The argument data
will have been changed such that
value
has been added as rows. If a traditional return object is desired,
and no side effects, then it can be called like a traditional function:
dat2 <- 'rbind<-'(dat1, value = add1).
attitude2 <- attitude rbind(attitude2) <- colMeans(attitude2) # defaults to rownames = as.character(nrow(`data`) + 1) attitude2 <- attitude2[!(`%in%`(x = row.names(attitude2), table = "31")), ] # logical subset rbind(attitude2, row.nm = "mean") <- colMeans(attitude2) attitude2 <- attitude2[-1*(match(x = "mean", table = row.names(attitude2))), ] # position subset rbind(attitude2, after = "10", row.nm = c("mean","sum")) <- rbind(colMeans(attitude2), colSums(attitude2)) # `value` as a matrix attitude2 <- attitude2[grep(pattern = "mean|sum", x = row.names(attitude2), invert = TRUE), ] # rownames subset attitude2 <- `rbind<-`(data = attitude2, value = colMeans(attitude2)) # traditional call attitude2 <- as.matrix(attitude, rownames.force = TRUE) # as.matrix.data.frame rbind(attitude2, after = "10", row.nm = "mean") <- colMeans(attitude2) # `data` as a matrix # using overwrite mtcars2 <- mtcars rownames(mtcars2) add <- mtcars[c("Mazda RX4","Mazda RX4 Wag","Datsun 710"), ]*11 rbind(mtcars2, overwrite = TRUE) <- add mtcars2 <- mtcars rbind(mtcars2, overwrite = FALSE) <- add
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.