matricks 0.8.2 available on CRAN

[This article was first published on krzjoa, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


matricks package in 0.8.2 version has been released on CRAN! In
this post I will present you, what are advantages of using matricks
and how you can use it.

Creating matrices

The main function the package started with is m. It’s a smart shortcut
for creating matrices, especially usefull if you want to define a matrix
by enumerating all the elements row-by-row. Typically, if you want to
create a matrix in R, you can do it using base function called
matrix.

<span class="n">matrix</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="m">3</span><span class="w"> </span><span class="p">,</span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">7</span><span class="p">,</span><span class="w">
         </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="p">,</span><span class="w">
         </span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">),</span><span class="w"> </span><span class="n">nrow</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="n">byrow</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    3    4    7
## [2,]    5    8    0
## [3,]    9    2    1

Although it’s a very simple opeartion, the funtion call doesn’t look
tidy. Alternaively, we can use tibble with its frame_matrix
function, defining column names with formulae first.

<span class="n">library</span><span class="p">(</span><span class="n">tibble</span><span class="p">)</span><span class="w">
</span><span class="n">frame_matrix</span><span class="p">(</span><span class="o">~</span><span class="w"> </span><span class="n">c1</span><span class="p">,</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">c2</span><span class="p">,</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">c3</span><span class="p">,</span><span class="w">
                </span><span class="m">3</span><span class="p">,</span><span class="w">    </span><span class="m">4</span><span class="p">,</span><span class="w">    </span><span class="m">7</span><span class="p">,</span><span class="w">
                </span><span class="m">5</span><span class="p">,</span><span class="w">    </span><span class="m">8</span><span class="p">,</span><span class="w">    </span><span class="m">0</span><span class="p">,</span><span class="w">
                </span><span class="m">9</span><span class="p">,</span><span class="w">    </span><span class="m">2</span><span class="p">,</span><span class="w">    </span><span class="m">1</span><span class="p">)</span><span class="w">
</span>
##      c1 c2 c3
## [1,]  3  4  7
## [2,]  5  8  0
## [3,]  9  2  1

However, it’s still not a such powerfull tool as matricks::m function
is. Let’s see an example.

<span class="n">library</span><span class="p">(</span><span class="n">matricks</span><span class="p">)</span><span class="w">
</span><span class="n">m</span><span class="p">(</span><span class="m">3</span><span class="w"> </span><span class="p">,</span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">7</span><span class="o">|</span><span class="w">
  </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="o">|</span><span class="w">
  </span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    3    4    7
## [2,]    5    8    0
## [3,]    9    2    1

As simple as that! We join following rows using | operator. m
function is very flexible and offers you much more than before mentioned
ones.

<span class="n">m</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">3</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">6</span><span class="p">,</span><span class="w"> </span><span class="m">7</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">4</span><span class="p">)</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    6    7
## [3,]    2    1    4

And here and example with bindig multiple matrices together:

<span class="n">mat1</span><span class="w">   </span><span class="o"><-</span><span class="w"> </span><span class="n">diag</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w">
</span><span class="n">mat2</span><span class="w">  </span><span class="o"><-</span><span class="w"> </span><span class="n">antidiag</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="m">3</span><span class="w">
</span><span class="n">m</span><span class="p">(</span><span class="n">mat1</span><span class="p">,</span><span class="w"> </span><span class="n">mat2</span><span class="o">|</span><span class="w">
  </span><span class="n">mat2</span><span class="p">,</span><span class="w"> </span><span class="n">mat1</span><span class="p">)</span><span class="w">
</span>
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    0    0    0    0    3
## [2,]    0    1    0    0    3    0
## [3,]    0    0    1    3    0    0
## [4,]    0    0    3    1    0    0
## [5,]    0    3    0    0    1    0
## [6,]    3    0    0    0    0    1

By the way, antidiag function can be found in the matricks package
too.

Setting & accessing values

These code

<span class="n">mat</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">matrix</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w">
</span><span class="n">mat</span><span class="p">[</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">0.3</span><span class="w">
</span><span class="n">mat</span><span class="p">[</span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">7</span><span class="w">
</span><span class="n">mat</span><span class="p">[</span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">13</span><span class="w">
</span><span class="n">mat</span><span class="p">[</span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">0.5</span><span class="w">
</span><span class="n">mat</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    0  0.3    0
## [2,]    0  0.5    7
## [3,]   13  0.0    0

can be replaced with:

<span class="n">mat</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">matrix</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w">
</span><span class="n">mat</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">set_values</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span><span class="w">
                  </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="m">0.3</span><span class="p">,</span><span class="w">
                  </span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="m">7</span><span class="p">,</span><span class="w">
                  </span><span class="nf">c</span><span class="p">(</span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="m">13</span><span class="p">,</span><span class="w">
                  </span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="m">0.5</span><span class="p">)</span><span class="w">
</span><span class="n">mat</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    0  0.3    0
## [2,]    0  0.5    7
## [3,]   13  0.0    0

In some cases, traditional way we access a matrix element in R may be
inconvenient. Consider situation shown below:

<span class="n">sample.matrix</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">matrix</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w">
</span><span class="n">matrix.indices</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">list</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">),</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">),</span><span class="w">
                       </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">),</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">),</span><span class="w">
                       </span><span class="nf">c</span><span class="p">(</span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">),</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">))</span><span class="w">

</span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">idx</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">matrix.indices</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">sample.matrix</span><span class="p">[</span><span class="n">idx</span><span class="p">[</span><span class="m">1</span><span class="p">],</span><span class="w"> </span><span class="n">idx</span><span class="p">[</span><span class="m">2</span><span class="p">]]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">sample.matrix</span><span class="p">[</span><span class="n">idx</span><span class="p">[</span><span class="m">1</span><span class="p">],</span><span class="w"> </span><span class="n">idx</span><span class="p">[</span><span class="m">2</span><span class="p">]]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">2</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">sample.matrix</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    3    3    3
## [2,]    1    3    1
## [3,]    3    1    3

It can be expressed conciser using matrix at function.

<span class="n">sample.matrix</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">matrix</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w">
</span><span class="n">matrix.indices</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">list</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">),</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">),</span><span class="w">
                       </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">),</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">),</span><span class="w">
                       </span><span class="nf">c</span><span class="p">(</span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">),</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">))</span><span class="w">

</span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">idx</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">matrix.indices</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">at</span><span class="p">(</span><span class="n">sample.matrix</span><span class="p">,</span><span class="w"> </span><span class="n">idx</span><span class="p">)</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">at</span><span class="p">(</span><span class="n">sample.matrix</span><span class="p">,</span><span class="w"> </span><span class="n">idx</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">2</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">sample.matrix</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    3    3    3
## [2,]    1    3    1
## [3,]    3    1    3

Plotting matrix

matrix objects haven’t had good automatized plotting function until
now. Let’s create and plot a sample matrix of random values.

<span class="n">rmat</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">runifm</span><span class="p">(</span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w">
</span><span class="n">print</span><span class="p">(</span><span class="n">rmat</span><span class="p">)</span><span class="w">
</span>
##           [,1]      [,2]      [,3]
## [1,] 0.3248890 0.1024049 0.3295454
## [2,] 0.8077164 0.7267801 0.1116789
## [3,] 0.4406909 0.4703106 0.7047498
<span class="n">plot</span><span class="p">(</span><span class="n">rmat</span><span class="p">)</span><span class="w">
</span>

png

And here the same using a matrix of random boolean values (rboolm).

<span class="n">set.seed</span><span class="p">(</span><span class="m">7</span><span class="p">)</span><span class="w">
</span><span class="n">rmat</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rboolm</span><span class="p">(</span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">)</span><span class="w">
</span><span class="n">print</span><span class="p">(</span><span class="n">rmat</span><span class="p">)</span><span class="w">
</span>
##       [,1]  [,2]  [,3]
## [1,] FALSE  TRUE  TRUE
## [2,]  TRUE  TRUE FALSE
## [3,]  TRUE FALSE  TRUE
<span class="n">plot</span><span class="p">(</span><span class="n">rmat</span><span class="p">)</span><span class="w">
</span>

png

Operators

matricks contains a family of operators, which allows you to perform
column-/row-wise operation
(addition/subtraction/multiplication/division) between matrix and
vector.

<span class="n">mat</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">m</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="o">|</span><span class="w">
         </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="m">6</span><span class="o">|</span><span class="w">
         </span><span class="m">7</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">)</span><span class="w">
</span><span class="n">mat</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
<span class="n">vec</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">v</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">3</span><span class="p">)</span><span class="w">
</span><span class="n">vec</span><span class="w">
</span>
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3

If we try to do a column-wise multiplication, we ecounter a problem.

<span class="n">mat</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">vec</span><span class="w">
</span>
## Error in mat * vec: niezgodne tablice

We can bypass this error using %m% function. It does what we want!

<span class="n">mat</span><span class="w"> </span><span class="o">%m%</span><span class="w"> </span><span class="n">vec</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    8   10   12
## [3,]   21   24   27

There are also several other operators available.

<span class="n">mat</span><span class="w"> </span><span class="o">%d%</span><span class="w"> </span><span class="n">vec</span><span class="w">
</span>
##          [,1]     [,2] [,3]
## [1,] 1.000000 2.000000    3
## [2,] 2.000000 2.500000    3
## [3,] 2.333333 2.666667    3
<span class="n">mat</span><span class="w"> </span><span class="o">%+%</span><span class="w"> </span><span class="n">vec</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    2    3    4
## [2,]    6    7    8
## [3,]   10   11   12
<span class="n">mat</span><span class="w"> </span><span class="o">%-%</span><span class="w"> </span><span class="n">vec</span><span class="w">
</span>
##      [,1] [,2] [,3]
## [1,]    0    1    2
## [2,]    2    3    4
## [3,]    4    5    6

I encourage you to familiarize with matricks. Visit matrix
documentation
site and learn more!

To leave a comment for the author, please follow the link and comment on their blog: krzjoa.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)