Description Usage Arguments Details Examples
View source: R/transform-cols.R
transform_cols()
transforms specified matrix columns with a user-supplied
function.
1 2 3 4 5 6 7 | transform_cols(x, fns, ..., which.cols, drop, name.sep)
## S3 method for class 'CsparseMatrix'
transform_cols(x, fns, ..., which.cols, drop = FALSE, name.sep = NULL)
## S3 method for class 'matrix'
transform_cols(x, fns, ..., which.cols, drop = FALSE, name.sep = NULL)
|
x |
A |
fns |
A user-supplied function, or list of functions, to apply to the specified columns. |
... |
Additional arguments to pass to |
which.cols |
A numeric vector indicating column indices or a character vector indicating column names. |
drop |
A logical value. If the functions in |
name.sep |
A |
transform_cols()
is an S3 generic with methods for:
CsparseMatrix
matrix
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 | x <- Matrix::rsparsematrix(10, 4, .9)
colnames(x) <- paste0("x", 1:4)
x
# Convert columns in-place with a single function
transform_cols(x, fns = function(i) i^2, which.cols = 3:4)
transform_cols(x, fns = function(i) i^2, which.cols = c("x3", "x4"))
# Mutate new columns with a single function
transform_cols(x, fns = scale, which.cols = 3:4, name.sep = "scaled")
# Mutate new columns with a list of functions and names
transform_cols(
x,
fns = c(function(i) i^2, function(i) i^3),
which.cols = 3:4,
name.sep = c("squared", "cubed")
)
# Mutate new columns with a list of functions and names for each new column
transform_cols(
x,
fns = c(function(i) i^2, function(i) i^3),
which.cols = 3:4,
name.sep = list(paste0("squared", 1:2), paste0("cubed", 1:2))
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.