colapply: Applies a function to columns of a data.table

Description Usage Arguments Value Examples

View source: R/hello.R

Description

You can apply a function independently to many columns at the same time. You can apply a function to: - all columns - specified columns (with cols=c("col1","col2", ...)) - cols whose name match cols_pattern

Usage

1
2
colapply(data, fn, cols = c(), cols_pattern = c(), prefix = "",
  suffix = "", by = NULL, coltypes_filter_fn = NULL, inplace = F)

Arguments

data

- must be a data.table

fn

- function to apply to the columns

cols

- a character vector. If used only columns with such names will be used.

cols_pattern

- a character specifying the cols name pattern to which the function will be applied

prefix

- the prefix to apply to the resulting column names

suffix

- the suffix to apply to the resulting column names

by

- same as in data.table

inplace

(default==FALSE) - TRUE/FALSE

coltype_filter_fn

- a function that filters the coltypes object (applied at the end). Takes a coltypes data.table and returns it filtered.

Value

data.table with remapped columns

Examples

1
2
3
4
5
6
    capply(data.table(iris), typeof)
    capply(data.table(iris), mean, cols_pattern="Length|Width")
    capply(data.table(iris), unique, cols=c("Species"))
    capply(data.table(iris), unique, c("Species")) # the same - cols is the first optional argument
    capply(data.table(iris), unique, "Species") # if the vector has dimension 1, this is equivalent
    capply(data.table(iris), function(x){x^2}, cols_pattern="Length|Width", inplace=T) # square all numeric columns

AlbertoEAF/data.Rh documentation built on Feb. 8, 2021, 5:50 p.m.