collapse {miscset} | R Documentation |
Collapse objects
Description
Collapse objects as in the paste
function.
Usage
collapse(x, sep, ...)
## Default S3 method:
collapse(x, sep = "", ..., .unique = FALSE,
.sort = FALSE, .decreasing = FALSE)
## S3 method for class 'data.frame'
collapse(x, sep = "", by = names(x), ...,
.unique = FALSE, .sort = FALSE, .decreasing = FALSE, .unlist = FALSE,
.sortby = FALSE)
Arguments
x |
Any R object. |
sep |
A character string to separate value columns.
|
... |
Forwarded to or from other methods. |
.unique |
Logical, return only unique values. |
.sort |
Logical, sort the values. |
.decreasing |
Logical, if sorting, then by decreasing values. |
by |
Column names to split data frame by, before
applying collapse on each remaining column within each piece.
Using the default (all columns), then |
.unlist |
Logical, if value columns need to be unlisted before collapsing. |
.sortby |
Logical, sort the output on the |
Details
For the data.frame
method, x
is converted to a
data.table
before applying the piece- and column-wise collapses. If
the input is already inheriting from data.table
, then the class is
retained.
.sortby
is causing setkeyv(x, by)
to be applied to x
after converting to a data.table
.
Author(s)
Sven E. Templer
Examples
#
### some data
set.seed(12)
s <- s2 <- sample(LETTERS[1:4], 9, replace = TRUE)
s2[1:2] <- rev(s2[1:2])
d <- data.frame(group = rep(letters[c(3,1,2)], each = 3),
value = s,
level = factor(s2),
stringsAsFactors = FALSE)
### collapse vectors
collapse(letters)
collapse(1:3) # coerced to character
collapse(LETTERS[1:5], "-") # separated by '-'
### collapse data.frames
# by all columns (same as unique)
collapse(d)
# by a grouping column
collapse(d, by = 1)
# by multiple, but not all columns
collapse(d, by = c("group", "value"))
# return single row
collapse(d, by = 0)
# return single row, unique and sorted values
collapse(d, by = 0, .unique = TRUE, .sort = TRUE)
#