{"id":809,"date":"2018-05-19T12:02:45","date_gmt":"2018-05-19T10:02:45","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=809"},"modified":"2018-09-25T10:30:58","modified_gmt":"2018-09-25T08:30:58","slug":"e03","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/e03\/","title":{"rendered":"R &#8211; Exercise III"},"content":{"rendered":"<p>1) Create a matrix named <span class=\"crayon-inline\">m1<\/span> with three rows and five columns and all the numeric (integer) values from 6 to 20!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nm1 &lt;- matrix(6:20, nrow = 3, ncol = 5)\r\n\r\nm1\r\n##      [,1] [,2] [,3] [,4] [,5]\r\n## [1,]    6    9   12   15   18\r\n## [2,]    7   10   13   16   19\r\n## [3,]    8   11   14   17   20\r\n<\/pre>\n<p>2) Multiply all elements in <span class=\"crayon-inline\">m1<\/span> by 0.5! Overwrite the matrix <span class=\"crayon-inline\">m1<\/span> with the result!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nm1 &lt;- m1 * 0.5\r\n<\/pre>\n<p>3) Create another matrix <span class=\"crayon-inline\">m2<\/span> with one row and five columns and all the numeric (integer) values from 1 to 5!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nm2 &lt;- matrix(1:5, nrow = 1, ncol = 5)\r\n\r\nm2\r\n##      [,1] [,2] [,3] [,4] [,5]\r\n## [1,]    1    2    3    4    5\r\n<\/pre>\n<p>4) Calculate the sum of all elements in <span class=\"crayon-inline\">m2<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nsum(m2)\r\n## [1] 15\r\n<\/pre>\n<p>5) Combine <span class=\"crayon-inline\">m1<\/span> and <span class=\"crayon-inline\">m2<\/span> with <span class=\"crayon-inline\">rbing()<\/span>. Save the result as <span class=\"crayon-inline\">m3<\/span> and check the dimension of the new matrix!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nm3 &lt;- rbind(m1, m2)\r\n\r\nm3\r\n##      [,1] [,2] [,3] [,4] [,5]\r\n## [1,]  3.0  4.5  6.0  7.5  9.0\r\n## [2,]  3.5  5.0  6.5  8.0  9.5\r\n## [3,]  4.0  5.5  7.0  8.5 10.0\r\n## [4,]  1.0  2.0  3.0  4.0  5.0\r\n\r\ndim(m3)\r\n## [1] 4 5\r\n<\/pre>\n<p>6) Index the 5th column of <span class=\"crayon-inline\">m3<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nm3[ , 5]\r\n## [1]  9.0  9.5 10.0  5.0\r\n<\/pre>\n<p>7) Index the 2nd and 4th lines of <span class=\"crayon-inline\">m3<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nm3[ c(2, 4), ]\r\n##      [,1] [,2] [,3] [,4] [,5]\r\n## [1,]  3.5    5  6.5    8  9.5\r\n## [2,]  1.0    2  3.0    4  5.0\r\n<\/pre>\n<p>8) Calculate the sums for all columns in <span class=\"crayon-inline\">m3<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\ncolSums(m3)\r\n## [1] 11.5 17.0 22.5 28.0 33.5\r\n<\/pre>\n<p>9) Calculate the standard deviation for the 3rd column in <span class=\"crayon-inline\">m3<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nsd( m3[ , 3] )\r\n## [1] 1.796988\r\n<\/pre>\n<p>10) From <span class=\"crayon-inline\">m3<\/span>, index the element in the 2nd column and 2nd line and all eight adjacent elements! Save the result as <span class=\"crayon-inline\">m4<\/span> and examine its object class!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nm4 &lt;- m3[2:4, 2:4]\r\n\r\nm4\r\n##      [,1] [,2] [,3]\r\n## [1,]  5.0  6.5  8.0\r\n## [2,]  5.5  7.0  8.5\r\n## [3,]  2.0  3.0  4.0\r\n\r\nclass(m4)\r\n## [1] &quot;matrix&quot;\r\n<\/pre>\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: 30px;color:#6b9e1f\"><strong><em>Return to Crash Course<\/em><\/strong><\/span>\n<\/div>\n<p><\/button><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1) Create a matrix named m1 with three rows and five columns and all the numeric (integer) values from 6 to 20! m1 &lt;- matrix(6:20, nrow = 3, ncol = 5) m1 ## [,1] [,2] [,3] [,4] [,5] ## [1,] 6 9 12 15 18 ## [2,] 7 10 13 16 19 ## [3,] 8 &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/e03\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;R &#8211; Exercise 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-809","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/809","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=809"}],"version-history":[{"count":9,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/809\/revisions"}],"predecessor-version":[{"id":2473,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/809\/revisions\/2473"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}