Transform matrix columns
transform_cols.Rd
transform_cols()
transforms specified matrix columns with a user-supplied
function.
Usage
transform_cols(x, fns, ..., which.cols, drop, name.sep)
# S3 method for CsparseMatrix
transform_cols(x, fns, ..., which.cols, drop = FALSE, name.sep = NULL)
# S3 method for matrix
transform_cols(x, fns, ..., which.cols, drop = FALSE, name.sep = NULL)
Arguments
- x
A
matrix
orCsparseMatrix
.- fns
A user-supplied function, or list of functions, to apply to the specified columns.
- ...
Additional arguments to pass to
fns
. It is important to note that these arguments will be passed to every function infns
, and so should only include arguments that are relevant to each function.- which.cols
A numeric vector indicating column indices or a character vector indicating column names.
- drop
A logical value. If the functions in
fns
work with columnar matrices, then setdrop = FALSE
, otherwise if the functions infns
require a numeric vector, setdrop = TRUE
. When working with large sparse matrices, it is essential to setdrop = FALSE
, as the alternative will be much more memory intensive.- name.sep
A
NULL
value or a list corresponding to each element offns
. Ifname.sep
isNULL
the specified columns will be transformed in-place, providedlength(fns) == 1
. Iffns
has more than one element, andname.sep
isNULL
, it will default to a numeric vector that is equal tolength(fns)
. Ifname.sep
is provided as a list, each element of this list must contain a character vector of length 1 orncol(x)
that will be appended to existing column names to create new column names. Providing this argument ensures that the transformed columns will be appended as new matrix columns.
Examples
x <- Matrix::rsparsematrix(10, 4, .9)
colnames(x) <- paste0("x", 1:4)
x
#> 10 x 4 sparse Matrix of class "dgCMatrix"
#> x1 x2 x3 x4
#> [1,] 1.400 -2.00 . 1.50
#> [2,] 0.550 0.72 -0.77 -1.80
#> [3,] 0.042 -1.10 . .
#> [4,] -0.960 1.40 -1.40 0.19
#> [5,] 1.100 -0.47 1.50 -2.90
#> [6,] 0.038 0.81 -1.20 -0.72
#> [7,] 0.860 . -0.45 -1.10
#> [8,] -2.800 -1.60 -1.10 1.00
#> [9,] -0.570 -2.20 0.16 -0.55
#> [10,] -2.000 1.80 -1.00 -0.36
# Convert columns in-place with a single function
transform_cols(x, fns = function(i) i^2, which.cols = 3:4)
#> 10 x 4 sparse Matrix of class "dgCMatrix"
#> x1 x2 x3 x4
#> [1,] 1.400 -2.00 . 2.2500
#> [2,] 0.550 0.72 0.5929 3.2400
#> [3,] 0.042 -1.10 . .
#> [4,] -0.960 1.40 1.9600 0.0361
#> [5,] 1.100 -0.47 2.2500 8.4100
#> [6,] 0.038 0.81 1.4400 0.5184
#> [7,] 0.860 . 0.2025 1.2100
#> [8,] -2.800 -1.60 1.2100 1.0000
#> [9,] -0.570 -2.20 0.0256 0.3025
#> [10,] -2.000 1.80 1.0000 0.1296
transform_cols(x, fns = function(i) i^2, which.cols = c("x3", "x4"))
#> 10 x 4 sparse Matrix of class "dgCMatrix"
#> x1 x2 x3 x4
#> [1,] 1.400 -2.00 . 2.2500
#> [2,] 0.550 0.72 0.5929 3.2400
#> [3,] 0.042 -1.10 . .
#> [4,] -0.960 1.40 1.9600 0.0361
#> [5,] 1.100 -0.47 2.2500 8.4100
#> [6,] 0.038 0.81 1.4400 0.5184
#> [7,] 0.860 . 0.2025 1.2100
#> [8,] -2.800 -1.60 1.2100 1.0000
#> [9,] -0.570 -2.20 0.0256 0.3025
#> [10,] -2.000 1.80 1.0000 0.1296
# Mutate new columns with a single function
transform_cols(x, fns = scale, which.cols = 3:4, name.sep = "scaled")
#> 10 x 6 sparse Matrix of class "dgCMatrix"
#> x1 x2 x3 x4 x3_scaled x4_scaled
#> [1,] 1.400 -2.00 . 1.50 0.48772090 1.53676519
#> [2,] 0.550 0.72 -0.77 -1.80 -0.39384035 -1.03229516
#> [3,] 0.042 -1.10 . . 0.48772090 0.36901049
#> [4,] -0.960 1.40 -1.40 0.19 -1.11511773 0.51692608
#> [5,] 1.100 -0.47 1.50 -2.90 2.20504800 -1.88864861
#> [6,] 0.038 0.81 -1.20 -0.72 -0.88614079 -0.19151177
#> [7,] 0.860 . -0.45 -1.10 -0.02747723 -0.48734296
#> [8,] -2.800 -1.60 -1.10 1.00 -0.77165231 1.14751363
#> [9,] -0.570 -2.20 0.16 -0.55 0.67090245 -0.05916624
#> [10,] -2.000 1.80 -1.00 -0.36 -0.65716384 0.08874936
# 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")
)
#> 10 x 8 sparse Matrix of class "dgCMatrix"
#> x1 x2 x3 x4 x3_squared x4_squared x3_cubed x4_cubed
#> [1,] 1.400 -2.00 . 1.50 . 2.2500 . 3.375000
#> [2,] 0.550 0.72 -0.77 -1.80 0.5929 3.2400 -0.456533 -5.832000
#> [3,] 0.042 -1.10 . . . . . .
#> [4,] -0.960 1.40 -1.40 0.19 1.9600 0.0361 -2.744000 0.006859
#> [5,] 1.100 -0.47 1.50 -2.90 2.2500 8.4100 3.375000 -24.389000
#> [6,] 0.038 0.81 -1.20 -0.72 1.4400 0.5184 -1.728000 -0.373248
#> [7,] 0.860 . -0.45 -1.10 0.2025 1.2100 -0.091125 -1.331000
#> [8,] -2.800 -1.60 -1.10 1.00 1.2100 1.0000 -1.331000 1.000000
#> [9,] -0.570 -2.20 0.16 -0.55 0.0256 0.3025 0.004096 -0.166375
#> [10,] -2.000 1.80 -1.00 -0.36 1.0000 0.1296 -1.000000 -0.046656
# 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))
)
#> 10 x 8 sparse Matrix of class "dgCMatrix"
#> x1 x2 x3 x4 x3_squared1 x4_squared2 x3_cubed1 x4_cubed2
#> [1,] 1.400 -2.00 . 1.50 . 2.2500 . 3.375000
#> [2,] 0.550 0.72 -0.77 -1.80 0.5929 3.2400 -0.456533 -5.832000
#> [3,] 0.042 -1.10 . . . . . .
#> [4,] -0.960 1.40 -1.40 0.19 1.9600 0.0361 -2.744000 0.006859
#> [5,] 1.100 -0.47 1.50 -2.90 2.2500 8.4100 3.375000 -24.389000
#> [6,] 0.038 0.81 -1.20 -0.72 1.4400 0.5184 -1.728000 -0.373248
#> [7,] 0.860 . -0.45 -1.10 0.2025 1.2100 -0.091125 -1.331000
#> [8,] -2.800 -1.60 -1.10 1.00 1.2100 1.0000 -1.331000 1.000000
#> [9,] -0.570 -2.20 0.16 -0.55 0.0256 0.3025 0.004096 -0.166375
#> [10,] -2.000 1.80 -1.00 -0.36 1.0000 0.1296 -1.000000 -0.046656