Description Usage Arguments Value Examples
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
1 2 |
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. |
data.table with remapped columns
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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.