grbase-utilities.Rd
Various utility functions for gRbase. Includes 'faster versions' of certain standard R functions.
rhsFormula2list(form)
rhsf2list(form)
rhsf2vec(form)
listify_dots(dots)
list2rhsFormula(form)
list2rhsf(form)
rowmat2list(X)
colmat2list(X)
matrix2list(X, byrow = TRUE)
which.arr.index(X)
which_matrix_index(X)
rowSumsPrim(X)
colSumsPrim(X)
colwiseProd(v, X)
lapplyV2I(setlist, item)
lapplyI2V(setlist, item)
which.arr.ind
: Returns matrix n x 2 matrix with
indices of non-zero entries in matrix X
. Notice
which_matrix_index__
is cpp implementation.
colwiseProd
: multiplies a vector v and a matrix X
columnwise (as opposed to rowwise which is achieved by
v * X
). Hence colwiseProd
does the same as
t(v * t(X))
- but it does so faster for numeric values.
lapplyV2I: same as but much faster than lapply(setlist, function(elt) match(elt, item))
lapplyI2V: same as but faster than lapply(setlist, function(elt) item[elt])
## colwiseProd
X <- matrix(1:16, nrow=4)
v <- 1:4
t(v * t(X))
#> [,1] [,2] [,3] [,4]
#> [1,] 1 10 27 52
#> [2,] 2 12 30 56
#> [3,] 3 14 33 60
#> [4,] 4 16 36 64
colwiseProd(v, X)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 10 27 52
#> [2,] 2 12 30 56
#> [3,] 3 14 33 60
#> [4,] 4 16 36 64
if (FALSE) { # \dontrun{
system.time(for (ii in 1:100000) t(v * t(X)))
system.time(for (ii in 1:100000) colwiseProd(v, X))
} # }
setlist <- list(c(1,2,3), c(2,3,4), c(2,4,5))
item <- c(2,3)
lapplyV2I(setlist, item)
#> [[1]]
#> [1] NA 1 2
#>
#> [[2]]
#> [1] 1 2 NA
#>
#> [[3]]
#> [1] 1 NA NA
#>
lapply(setlist, function(gg) match(gg, item))
#> [[1]]
#> [1] NA 1 2
#>
#> [[2]]
#> [1] 1 2 NA
#>
#> [[3]]
#> [1] 1 NA NA
#>
lapplyI2V(setlist, item)
#> [[1]]
#> [1] 2 3 NA
#>
#> [[2]]
#> [1] 3 NA NA
#>
#> [[3]]
#> [1] 3 NA NA
#>
lapply(setlist, function(x) item[x])
#> [[1]]
#> [1] 2 3 NA
#>
#> [[2]]
#> [1] 3 NA NA
#>
#> [[3]]
#> [1] 3 NA NA
#>
if (require(microbenchmark)){
microbenchmark(
lapplyV2I(setlist, item),
lapply(setlist, function(elt) match(elt, item)))
microbenchmark::microbenchmark(
lapplyI2V(setlist, item),
lapply(setlist, function(elt) item[elt]))
}
#> Unit: microseconds
#> expr min lq mean median uq
#> lapplyI2V(setlist, item) 1.707 1.7825 1.93210 1.8790 1.9515
#> lapply(setlist, function(elt) item[elt]) 1.573 1.6420 1.86108 1.7295 1.8430
#> max neval cld
#> 5.872 100 a
#> 11.289 100 a