Apply Functions Over Sparse Matrix Margins
apply.Rd
Returns a vector or array or list of values obtained by applying a function to margins of a sparse matrix.
Details
This is a handy wrapper to apply a function to the (nonzero)
elements of a sparse matrix.
For example, it is possible to apply a covariance matrix to a distance
matrix obtained by nearest.dist
, see Examples.
A call to apply
only coerces the sparse matrix to a regular one.
The basic principle is applying the function to @entries
, or to
the extracted columns or rows ([,i,drop=F]
or
[i,,drop=F]
). It is important to note that an empty column
contains at least one zero value and may lead to non intuitive
results.
This function may evolve over the next few releases.
Value
Similar as a call to apply
with a regular matrix. The most
important cases are as follows. The
result is a vector (MARGIN
is length 1 and FUN
is
scalar) or a matrix (MARGIN
is length 1 and FUN
returns
fixed length vectors, or MARGIN
is length 2 and FUN
is
scalar) or a list (if FUN
returns vectors of different lengths).
Examples
S <- as.spam(dist(1:5))
S <- apply.spam(S/2, NULL, exp)
# instead of
# S@entries <- exp( S@entries/2)
# Technical detail, a null matrix consists
# of one zero element.
apply.spam(S,c(1,2),pmax)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.000000 0.000000 0.000000 0.000000 0
#> [2,] 1.648721 0.000000 0.000000 0.000000 0
#> [3,] 2.718282 1.648721 0.000000 0.000000 0
#> [4,] 4.481689 2.718282 1.648721 0.000000 0
#> [5,] 7.389056 4.481689 2.718282 1.648721 0
#> Class 'spam' (32-bit)
apply.spam(S,1,range)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0 1.648721 1.648721 1.648721 1.648721
#> [2,] 0 1.648721 2.718282 4.481689 7.389056
# A similar example as for the base apply.
# However, no dimnames else we would get warnings.
x <- as.spam(cbind(x1 = 3, x2 = c(0,0,0, 5:2)))
apply.spam(x, 2, mean, trim = .2)
#> [1] 3.0 3.5
col.sums <- apply.spam(x, 2, sum)
row.sums <- apply.spam(x, 1, sum)
rbind(cbind(x, row.sums), c(col.sums, sum(col.sums)))
#> [,1] [,2] [,3]
#> [1,] 3 0 3
#> [2,] 3 0 3
#> [3,] 3 0 3
#> [4,] 3 5 8
#> [5,] 3 4 7
#> [6,] 3 3 6
#> [7,] 3 2 5
#> [8,] 21 14 35
#> Class 'spam' (32-bit)
apply.spam(x, 2, is.vector)
#> [1] TRUE TRUE
# Sort the columns of a matrix
# Notice that the result is a list due to the different
# lengths induced by the nonzero elements
apply.spam(x, 2, sort)
#> [[1]]
#> [1] 3 3 3 3 3 3 3
#>
#> [[2]]
#> [1] 2 3 4 5
#>
# Function with extra args:
cave <- function(x, c1, c2) c(mean(x[c1]), mean(x[c2]))
apply(x,1, cave, c1=1, c2=c(1,2))
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] 3.0 3.0 3.0 3 3.0 3 3.0
#> [2,] 1.5 1.5 1.5 4 3.5 3 2.5
ma <- spam(c(1:4, 0, 0,0, 6), nrow = 2)
ma
#> [,1] [,2] [,3] [,4]
#> [1,] 1 3 0 0
#> [2,] 2 4 0 6
#> Class 'spam' (32-bit)
apply.spam(ma, 1, table) #--> a list of length 2
#> [[1]]
#>
#> 1 3
#> 1 1
#>
#> [[2]]
#>
#> 2 4 6
#> 1 1 1
#>
apply.spam(ma, 1, stats::quantile)# 5 x n matrix with rownames
#> [,1] [,2]
#> 0% 1.0 2
#> 25% 1.5 3
#> 50% 2.0 4
#> 75% 2.5 5
#> 100% 3.0 6