reduce,data.frame-method {MSnbase} | R Documentation

## Reduce a data.frame

### Description

Reduce a data.frame so that the (primary) key column contains only unique entries and other columns pertaining to that entry are combined into semicolon-separated values into a single row/observation.

### Usage

    ## S4 method for signature 'data.frame'
    reduce(x, key, sep = ";")

### Arguments

`x` | A `data.frame`.
`key` | The column name (currenly only one is supported) to be used as primary key.
`sep` | The separator. Default is `;`.

### Details

An important side-effect of reducing a `data.frame` is that all columns other than the key are converted to characters when they are collapsed to a semi-column separated value (even if only one value is present) as soon as one observation of transformed.

### Value

A reduced `data.frame`.

### Author(s)

Laurent Gatto

### Examples

    dfr <- data.frame(A = c(1, 1, 2),
                      B = c("x", "x", "z"),
                      C = LETTERS[1:3])
    dfr
    dfr2 <- reduce(dfr, key = "A")
    dfr2
    ## column A used as key is still num
    str(dfr2)
    dfr3 <- reduce(dfr, key = "B")
    dfr3
    ## A is converted to chr; B remains factor
    str(dfr3)
    dfr4 <- data.frame(A = 1:3,
                       B = LETTERS[1:3],
                       C = c(TRUE, FALSE, NA))
    ## No effect of reducing, column classes are maintained
    str(reduce(dfr4, key = "B"))

[Package MSnbase version 2.20.1 Index]