collapse: Collapse objects

Description Usage Arguments Details Author(s) Examples

Description

Collapse objects as in the paste function.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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. NULL retains a vector.

...

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 unique(x) is returned. Columns can be specified by names or integer with the column numbers. Using 0 or NULL collapses all columns.

.unlist

Logical, if value columns need to be unlisted before collapsing.

.sortby

Logical, sort the output on the by columns. This applies , If x was a data.table, then the keys are set as the by values.

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

 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)

#

setempler/miscset documentation built on May 29, 2019, 8 p.m.