knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
Useful tricks for matrix manipulation
matricks
is available on CRAN, so you can install it using simply:
install.packages('matricks')
If you rather want to install dev version, you can do it with devtools
.
devtools::install_github('krzjoa/matricks')
Main matricks
functions are m
and v
, which provide convenient API to create matrices and vectors.
Why should we write:
matrix(c(5, 6, 7,
8, 0, 9,
3, 7, 1), nrow = 3, byrow = TRUE)
if we can simply create such a matrix like that:
library(matricks)
m(5, 6, 7|
8, 0, 9|
3, 7, 1)
v
function is an useful shortcut for creating vertical vectors (single columns)
v(1,2,3)
v(1:5)
Setting values in easier with matricks
mat <- matrix(0, 3, 3)
set_values(mat, c(1, 2) ~ 0.5, c(3, 1) ~ 7)