Description Usage Arguments Details Author(s) Examples
Collapse objects as in the paste
function.
1 2 3 4 5 6 7 8 9 10 |
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 |
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
.
Sven E. Templer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #
### 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)
#
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.