Matrices and arrays

TODO

  • link to matrixArithmetic

Matrices

Create a matrix using matrix()

     [,1] [,2]
[1,]   17   25
[2,]   30   23
[3,]   30   21
     [,1] [,2] [,3]
[1,]   17   30   30
[2,]   25   23   21

Combine rows and columns to a matrix

       [,1] [,2] [,3] [,4] [,5]
age      19   19   31   19   24
weight   95   76   94   76   76
height  197  178  189  184  173
     age weight height
[1,]  19     95    197
[2,]  19     76    178
[3,]  31     94    189
[4,]  19     76    184
[5,]  24     76    173

Information about matrices

[1] 2 3
[1] 2
[1] 3
[1] 6

Transpose matrices

     [,1] [,2]
[1,]   17   25
[2,]   30   23
[3,]   30   21
     [,1]
[1,]    1
[2,]    2
[3,]    3
[1] 17 25 30 23 30 21

Rows and columns

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    2    3
     rowIdx colIdx val
[1,]      1      1   1
[2,]      2      1   2
[3,]      1      2   3
[4,]      2      2   4
[5,]      1      3   5
[6,]      2      3   6
      [,1]  [,2]  [,3]  [,4]
[1,] FALSE  TRUE  TRUE  TRUE
[2,] FALSE FALSE  TRUE  TRUE
[3,] FALSE FALSE FALSE  TRUE
[4,] FALSE FALSE FALSE FALSE
      [,1]  [,2]  [,3]  [,4]
[1,] FALSE FALSE FALSE FALSE
[2,]  TRUE FALSE FALSE FALSE
[3,]  TRUE  TRUE FALSE FALSE
[4,]  TRUE  TRUE  TRUE FALSE

Extract and change elements

Row and column indices

[1] 20
[1] 24
[1] 24 24 16
[1] 32 24
     [,1] [,2] [,3]
[1,]   32   19   19
[2,]   24   24   16
     [,1]
[1,]   32
[2,]   24
     [,1] [,2]
[1,]   19   19
[2,]   24   16
     [,1] [,2]
[1,]   32   19
[2,]   24   16

Index vector

[1] 32 19 24

Index matrix

     [,1] [,2]
[1,]   11   12
[2,]   21   22
     [,1] [,2] [,3]
[1,]   11   19   12
[2,]   21   24   22

Logical index matrix

      [,1]  [,2]  [,3]
[1,]  TRUE FALSE FALSE
[2,] FALSE FALSE FALSE
[1] 32
     row col
[1,]   1   1
[1] 32
     [,1] [,2]
[1,]    1    1
[2,]    1    2
[3,]    2    2

Also see help(Extract)

Sort matrices

[1] 1 2 4 5 3
     age weight height
[1,]  19     95    197
[2,]  19     76    178
[3,]  19     76    184
[4,]  24     76    173
[5,]  31     94    189
     age weight height
[1,]  19     76    178
[2,]  19     76    184
[3,]  19     95    197
[4,]  24     76    173
[5,]  31     94    189
     age weight height
[1,]  19     76    184
[2,]  19     76    178
[3,]  24     76    173
[4,]  31     94    189
[5,]  19     95    197

Arrays

Create arrays

, , layer = high

   column
row CG WL T
  f  1  3 5
  m  2  4 6

, , layer = low

   column
row CG WL  T
  f  7  9 11
  m  8 10 12

Extract and change elements

[1] 11
   column
row CG WL  T
  f  2  6 10
  m  4  8 12

Transpose arrays

Switch rows and columns

, , layer = high

      row
column f m
    CG 1 2
    WL 3 4
    T  5 6

, , layer = low

      row
column  f  m
    CG  7 19
    WL  9 10
    T  11 12

Switch rows and layers

, , row = f

      column
layer  CG WL  T
  high  1  3  5
  low   7  9 11

, , row = m

      column
layer  CG WL  T
  high  2  4  6
  low  19 10 12

Get the article source from GitHub

R markdown - markdown - R code - all posts