Learn R Programming

Kmisc (version 0.5.0)

stack_list: Stack a List of DataFrame-like Objects

Description

Function for stacking a list, where each component of the list is a data.frame containing potentially differing number of rows, but the same number of columns, and with all columns of equivalent class.

Usage

stack_list(list, name = "row_names", make_row_names = typeof(attr(list[[1]], "row.names")) == "character", keep_list_index = TRUE, index_name = "list_index", coerce_factors = TRUE)

Arguments

list
a list of data frames, or a list of lists with each element of the same length.
name
a name to assign to the column of row names generated.
make_row_names
boolean. add a column built from the row names? Defaults to TRUE only if the row names are of type character.
keep_list_index
boolean; if TRUE we include a vector that indicates which list a particular entry came from.
index_name
a name to assign to the column of indices.
coerce_factors
boolean; if TRUE, we convert factors to their character representation; otherwise, we take the internal integer representation.

Details

When stacking data.frames with row names, they are passed into a column called row_names to protect from problems with non-unique row names, and also to avoid appending numbers onto these row names as well.

Note that information on factors is lost by default; they will be converted either to characters or their internal integer representations.

Examples

Run this code
x <- data.frame( x=c(1, 2, 3), y=letters[1:3], z=rnorm(3), stringsAsFactors=FALSE )
rownames(x) <- c("apple", "banana", "cherry")
y <- data.frame( x=c(4, 5, 6), y=LETTERS[1:3], z=runif(3), stringsAsFactors=FALSE )
rownames(y) <- c("date", "eggplant", "fig")
tmp1 <- stack_list( list(x, y) )
tmp2 <- do.call( rbind, list(x, y) )
rownames(tmp2) <- 1:nrow(tmp2)
all.equal( tmp1[,1:3], tmp2 )

Run the code above in your browser using DataLab