bindx: Bind objects

bindxR Documentation

Bind objects

Description

Utilities for binding objects with inconsistent dimensions.

Usage

bind_all(..., which)

cbindx(..., deparse.level = 1L)

rbindx(..., deparse.level = 1L)

rbindfill(...)

rbindfill2(..., use.rownames = FALSE)

rbindlist(..., use.rownames = FALSE, use.names = FALSE)

rbindlist2(
  data,
  column,
  split = "\\W+",
  fixed = FALSE,
  perl = FALSE,
  use.rownames = any(rownames(data) != seq.int(nrow(data)))
)

Arguments

...

for bind_all and rbindfill, vectors; cbindx and rbindx will accept vectors, matrices, data frames; data frames should be used with rbindfill2 but matrices (or a combination) will work, but a data frame is returned; rbindlist accepts vectors or one or more lists

which

joining method; 'rbind' or 'cbind'

deparse.level

integer controlling the construction of labels in the case of non-matrix-like arguments (for the default method):
deparse.level = 0 constructs no labels; the default;
deparse.level = 1 or 2 constructs labels from the argument names
see cbind

use.rownames

logical; for rbindfill2, if TRUE, data frames in a named list will retain corresponding rownames; the default is to remove rownames (note that this parameter is ignored if ... is not a named list)

for rbindlist and rbindlist2, return a data frame with or without rownames; for rbindlist2, if data has no row names set (i.e., are "1", "2", ...), then the default is FALSE

use.names

logical; if TRUE and vectors are named, names are preserved and added as a column

data

a matrix or data frame

column

for rbindlist2, the column(s) to be unnested

split, fixed, perl

arguments passed to strsplit controlling how nested column text should be split

Details

bind_all and rbindfill are used for binding vectors, the latter specifically for rbinding named vectors a la a "stacking" merge.

cbindx and rbindx take vector-, matrix-, and data frame-like objects and bind normally, filling with NAs where dimensions are not equal.

rbindfill stacks named vectors by initializing a matrix with all names and filling with ... by row in the order given. The column names will be a vector of the unique names in the order they were given.

rbindfill2 row-binds data frames with zero or more common column names. rbindfill2 starts with the first data frame given and rbinds subsequent data frames adding new columns of NA as needed to bind. Any columns with matching names will be aggregated; otherwise, data frames without a matching column of data will be filled with NA.

rbindlist converts a list of vectors into a long data frame with two columns: the list index where each value was stored and the values themselves. A third column will be added if use.names = TRUE which will keep the names of each vector.

rbindlist2 uses rbindlist to expand data frames with one or more nested columns.

See Also

cbind; rbind; interleave; clist; qpcR

Examples

bind_all(1:5, 1:3, which = 'cbind')
bind_all(1:5, 1:3, which = 'rbind')

m1 <- matrix(1:4)
m2 <- matrix(1:4, 1)

cbindx(m1, m2)
rbindx(m1, m2)
rbindx(mtcars, m2)


## "stack" named vectors
f <- function(x) setNames(letters[x], LETTERS[x])
x <- lapply(list(1:5, 3:6, 2:7, 26), f)
do.call('rbindfill', x)

## "stack" matrices or data frames
colnames(m1) <- 'B'
colnames(m2) <- LETTERS[1:4]
rbindfill2(m1, m2)
rbindfill2(m2, m1)

set.seed(1)
dd <- matrix(NA, nrow = 1, ncol = 10)
dd <- as.data.frame(col(dd))
l <- setNames(lapply(1:5, function(x) dd[, sample(x), drop = FALSE]),
              letters[1:5])

Reduce('rbindfill2', l) ## or do.call('rbindfill2', l)
do.call('rbindfill2', c(l, use.rownames = TRUE))
rbindfill2(l$c, l$e)

rbindfill2(head(mtcars), head(cars))


## "stack" a list of vectors with differing lengths
rbindlist(1:5)
rbindlist(1:5, 1:5)

l <- lapply(1:4, sequence)
rbindlist(l)
rbindlist(l[4L])

names(l) <- LETTERS[1:4]
rbindlist(l)
rbindlist(l[4L])

l <- lapply(l, function(x) setNames(x, letters[x]))
rbindlist(l, use.names = TRUE)
rbindlist(unname(l), use.names = TRUE)


## unnest and stack a data frame or matrix
dd <- data.frame(
  id = 1:4, x = rnorm(4), y = sapply(l, toString),
  z = sapply(l, paste, collapse = '...')
)
rbindlist2(dd, 'y')

## rownames are kept if data contains non default rownames
rbindlist2(`rownames<-`(dd, letters[1:4]), 'y')

## multiple columns can be expanded sequentially
rbindlist2(dd, c('y', 'z'))
rbindlist2(dd, 'y', split = '\\W+(?![^3]+3)', perl = TRUE)


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