Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

reduce-data.frame-method

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"))

MSnbase

Base Functions and Classes for Mass Spectrometry and Proteomics

v2.16.1
Artistic-2.0
Authors
Laurent Gatto, Johannes Rainer and Sebastian Gibb with contributions from Guangchuang Yu, Samuel Wieczorek, Vasile-Cosmin Lazar, Vladislav Petyuk, Thomas Naake, Richie Cotton, Arne Smits, Martina Fisher, Ludger Goeminne, Adriaan Sticker and Lieven Clement.
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.