DataFrame-combine | R Documentation |
Various methods are provided to combine DataFrame objects along their rows or columns, or to merge them.
In the code snippets below, all the input objects are expected to be DataFrame objects.
rbind(...)
: Creates a new DataFrame object by
aggregating the rows of the input objects.
Very similar to rbind.data.frame()
, except
in the handling of row names. If all elements have row names, they
are concatenated and made unique. Otherwise, the result does not
have row names.
The returned DataFrame object inherits its metadata and
metadata columns from the first input object.
cbind(...)
: Creates a new DataFrame object by
aggregating the columns of the input objects.
Very similar to cbind.data.frame()
.
The returned DataFrame object inherits its metadata from
the first input object.
The metadata columns of the returned DataFrame object are
obtained by combining the metadata columns of the input object with
combineRows()
.
combineRows(x, ...)
:combineRows()
is a generic
function documented in the man page for RectangularData
objects (see ?RectangularData
).
The method for DataFrame objects behaves as documented in
that man page.
combineCols(x, ..., use.names=TRUE)
:combineCols()
is a generic function documented in the man page for
RectangularData objects (see ?RectangularData
).
The method for DataFrame objects behaves as documented in
that man page.
combineUniqueCols(x, ..., use.names=TRUE)
: This function
is documented in the man page for RectangularData objects
(see ?RectangularData
).
merge(x, y, ...)
: Merges two DataFrame objects
x
and y
, with arguments in ...
being
the same as those allowed by the base merge()
. It is
allowed for either x
or y
to be a data.frame
.
Michael Lawrence, Hervé Pagès, and Aaron Lun
DataFrame-utils for other common operations on DataFrame objects.
DataFrame objects.
TransposedDataFrame objects.
RectangularData objects.
cbind
and merge
in the base
package.
## ---------------------------------------------------------------------
## rbind()
## ---------------------------------------------------------------------
x1 <- DataFrame(A=1:5, B=letters[1:5], C=11:15)
y1 <- DataFrame(B=c(FALSE, NA, TRUE), C=c(FALSE, NA, TRUE), A=101:103)
rbind(x1, y1)
x2 <- DataFrame(A=Rle(101:103, 3:1), B=Rle(51:52, c(1, 5)))
y2 <- DataFrame(A=runif(2), B=Rle(c("a", "b")))
rbind(x2, y2)
## ---------------------------------------------------------------------
## combineRows()
## ---------------------------------------------------------------------
y3 <- DataFrame(A=runif(2))
combineRows(x2, y3)
y4 <- DataFrame(B=Rle(c("a", "b")), C=runif(2))
combineRows(x2, y4)
combineRows(y4, x2)
combineRows(y4, x2, DataFrame(D=letters[1:3], B=301:303))
## ---------------------------------------------------------------------
## combineCols()
## ---------------------------------------------------------------------
X <- DataFrame(x=1)
Y <- DataFrame(y="A")
Z <- DataFrame(z=TRUE)
combineCols(X, Y, Z, use.names=FALSE)
Y <- DataFrame(y=LETTERS[1:2])
rownames(X) <- "foo"
rownames(Y) <- c("foo", "bar")
rownames(Z) <- "bar"
combineCols(X, Y, Z)
## ---------------------------------------------------------------------
## combineUniqueCols()
## ---------------------------------------------------------------------
X <- DataFrame(x=1)
Y <- DataFrame(y=LETTERS[1:2], dup=1:2)
Z <- DataFrame(z=TRUE, dup=2L)
rownames(X) <- "foo"
rownames(Y) <- c("foo", "bar")
rownames(Z) <- "bar"
combineUniqueCols(X, Y, Z)
Z$dup <- 3
combineUniqueCols(X, Y, Z)
## ---------------------------------------------------------------------
## merge()
## ---------------------------------------------------------------------
x6 <- DataFrame(key=c(155, 2, 33, 17, 2, 26, 1), aa=1:7)
y6 <- DataFrame(key=1:26, bb=LETTERS)
merge(x6, y6, by="key")
merge(x6, y6, by="key", all.x=TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.