{"id":1456,"date":"2018-06-21T23:49:16","date_gmt":"2018-06-21T21:49:16","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=1456"},"modified":"2018-09-25T10:30:36","modified_gmt":"2018-09-25T08:30:36","slug":"r-crash-course-3","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/r-crash-course-3\/","title":{"rendered":"R Crash Course Part III"},"content":{"rendered":"<p><a name=\"ch6\"><\/a><\/p>\n<h1>6. Matrices<\/h1>\n<p>A matrix is a two-dimensional data structure and only contains elements of a single class. It can be created via <span class=\"crayon-inline\">matrix()<\/span> by defining a number of rows <span class=\"crayon-inline\">nrow<\/span> and columns <span class=\"crayon-inline\">ncol<\/span> of a given vector:<\/p>\n<pre class=\"theme:amityreseda\">\r\nm &lt;- matrix(1:6, nrow = 3, ncol = 2) \r\nm\r\n##      [,1] [,2]\r\n## [1,]    1    4\r\n## [2,]    2    5\r\n## [3,]    3    6\r\n\r\nlength(m)      # shows number of elements in matrix\r\n## [1] 6\r\n\r\ndim(m)         # shows nrow und ncol attributes\r\n## [1] 3 2\r\n<\/pre>\n<p>The colon in expression <span class=\"crayon-inline\">1:6<\/span> in line 1 is the sequential operator, which in this case creates a vector of all integer values between 1 and 6.<br \/>\nA matrix can also be constructed from vectors by using the dim () function to define the dimensionality:<\/p>\n<pre class=\"theme:amityreseda\">\r\na &lt;- 1:16\r\na\r\n##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16\r\n\r\ndim(a) &lt;- c(2,8)\r\na\r\n##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]\r\n## [1,]    1    3    5    7    9   11   13   15\r\n## [2,]    2    4    6    8   10   12   14   16\r\n<\/pre>\n<p>Furthermore, we can connect two vectors to each other via <span class=\"crayon-inline\">cbind()<\/span> (column bind) or <span class=\"crayon-inline\">rbind()<\/span> (row bind). If the vectors do not have the same length, they would be padded with NA values.<\/p>\n<pre class=\"theme:amityreseda\">\r\nx &lt;- c(23, 44, 15, 12)\r\ny &lt;- c( 1,  2,  3,  4)\r\n\r\nb &lt;- cbind(x, y)\r\nb\r\n##       x y\r\n## [1,] 23 1\r\n## [2,] 44 2\r\n## [3,] 15 3\r\n## [4,] 12 4\r\n\r\nc &lt;- rbind(x, y)\r\nc\r\n##   [,1] [,2] [,3] [,4]\r\n## x   23   44   15   12\r\n## y    1    2    3    4\r\n<\/pre>\n<p>If more than two dimensions are needed, e.g., when working with remote sensing imagery, we use arrays. These behave like matrices, but have at least three dimensions (<span class=\"crayon-inline\">help(array)<\/span>).<\/p>\n<p><strong>Indexing in matrices<\/strong><\/p>\n<p>Indexing in matrices behaves adequately to indexing in vectors, except that we now put two index numbers in the square brackets <span class=\"crayon-inline\">[]<\/span> to address rows and columns. Both numbers must always be separated by a comma <span class=\"crayon-inline\">[line, column]<\/span>. If we want all the entries from one dimension, we simply leave the corresponding slot for the index numbers empty:<\/p>\n<pre class=\"theme:amityreseda\">\r\nm &lt;- matrix(1:15, nrow = 5, ncol = 3) \r\nm\r\n##      [,1] [,2] [,3]\r\n## [1,]    1    6   11\r\n## [2,]    2    7   12\r\n## [3,]    3    8   13\r\n## [4,]    4    9   14\r\n## [5,]    5   10   15\r\n\r\nm[ , 2]                     # extract second column\r\n## [1]  6  7  8  9 10\r\n\r\nm[3,  ]                     # extract third row\r\n## [1]  3  8 13\r\n\r\nm[1, c(2, 3)]               # elements of first row in 2nd and 3rd column\r\n## [1]  6 11\r\n<\/pre>\n<p><strong>Calculate with matrices<\/strong><\/p>\n<p>R is an equally powerful tool in terms of linear algebra. Appropriate to the vectors, whole matrices can be multiplied by a single value (scalar multiplication) or element by element. For the latter, however, the matrices necessarily need the same dimensionality <span class=\"crayon-inline\">dim()<\/span>.<\/p>\n<pre class=\"theme:amityreseda\">\r\nm1 &lt;- matrix(1:8, nrow = 2)\r\nm1\r\n##      [,1] [,2] [,3] [,4]\r\n## [1,]    1    3    5    7\r\n## [2,]    2    4    6    8\r\n\r\nm1 * 5                         # scalar multiplication\r\n##      [,1] [,2] [,3] [,4]\r\n## [1,]    5   15   25   35\r\n## [2,]   10   20   30   40\r\n\r\nm1 * m1                        # multiplication element-wise\r\n##      [,1] [,2] [,3] [,4]\r\n## [1,]    1    9   25   49\r\n## [2,]    4   16   36   64\r\n<\/pre>\n<p>Some useful and commonly used functions:<\/p>\n<pre class=\"theme:amityreseda\">\r\nm2 &lt;- matrix(1:6, nrow = 2)\r\nm2\r\n##      [,1] [,2] [,3]\r\n## [1,]    1    3    5\r\n## [2,]    2    4    6\r\n\r\ncolMeans(m2)               # mean of all columns\r\n## [1] 1.5 3.5 5.5\r\n\r\ncolSums(m2)                # sum of all columns\r\n## [1]  3  7 11\r\n\r\nrowMeans(m2)               # mean of all rows\r\n## [1] 3 4\r\n\r\nrowSums(m2)                # sum of all rows\r\n## [1]  9 12\r\n\r\nt(m2)                      # transpose a matrix\r\n##      [,1] [,2]\r\n## [1,]    1    2\r\n## [2,]    3    4\r\n## [3,]    5    6\r\n\r\nm3 &lt;- matrix(1:6, ncol = 2)\r\nm3 %*% m2                  # matrix multiplication\r\n##      [,1] [,2] [,3]\r\n## [1,]    9   19   29\r\n## [2,]   12   26   40\r\n## [3,]   15   33   51\r\n<\/pre>\n<p>Matrix multiplications assume that the inner dimensions of the two matrices are the same length (<a href=\"https:\/\/de.wikipedia.org\/wiki\/Matrizenmultiplikation\">here<\/a> you will find further information).<\/p>\n<p>Next training session incoming:<\/p>\n<p><a target=\"_blank\" href=\"https:\/\/blogs.fu-berlin.de\/reseda\/e03\/\"><br \/>\n<button style=\"width:100%;text-align:center;padding: 0;background-color:#6b9e1f;color: white\"><\/p>\n<div style=\"font-family: 'Noto Sans',sans-serif\"><span style=\"font-size: 30px\"><strong>EXERCISE III<\/strong><\/span><\/div>\n<p><\/button><\/a><\/p>\n<hr style=\"height: 4px;background-color: #6b9e1f\" \/>\n<a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/r-crash-course-4\/\"><br \/>\n<button style=\"width:100%;text-align:right;padding: 10 0;background-color:white;margin:-55px 0 0 0\"><\/p>\n<div style=\"font-family: 'Noto Sans',sans-serif;line-height: 1.2\">\n<span style=\"font-size: 12px;color:#bfbfbf\"><strong><em>NEXT<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 30px;color:#6b9e1f\"><strong><em>R Crash Course Part IV<\/em><\/strong><\/span>\n<\/div>\n<p><\/button><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>6. Matrices A matrix is a two-dimensional data structure and only contains elements of a single class. It can be created via matrix() by defining a number of rows nrow and columns ncol of a given vector: m &lt;- matrix(1:6, nrow = 3, ncol = 2) m ## [,1] [,2] ## [1,] 1 4 ## &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/r-crash-course-3\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;R Crash Course Part III&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3237,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1456","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/users\/3237"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/comments?post=1456"}],"version-history":[{"count":11,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1456\/revisions"}],"predecessor-version":[{"id":2472,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1456\/revisions\/2472"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=1456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}