{"id":793,"date":"2018-05-18T23:32:54","date_gmt":"2018-05-18T21:32:54","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=793"},"modified":"2018-09-25T10:27:52","modified_gmt":"2018-09-25T08:27:52","slug":"e02","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/e02\/","title":{"rendered":"R &#8211; Exercise II"},"content":{"rendered":"<p>1) Create an integer variable <span class=\"crayon-inline\">e<\/span> holding the value <span class=\"crayon-inline\">42<\/span>! Check the object class of <span class=\"crayon-inline\">e<\/span> with <span class=\"crayon-inline\">class()<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\ne &lt;- 42L\r\n\r\nclass(e)\r\n## [1] &quot;integer&quot;\r\n<\/pre>\n<p>2) Convert <span class=\"crayon-inline\">e<\/span> to the character data type with <span class=\"crayon-inline\">as.character()<\/span>! Check the object class again!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\ne &lt;- as.character(e)\r\n\r\nclass(e)\r\n## [1] &quot;character&quot;\r\n<\/pre>\n<p>3) Create a character vector <span class=\"crayon-inline\">friends<\/span> with four names from your circle of friends or acquaintances!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nfriends &lt;- c(&quot;Anna&quot;, &quot;Otto&quot;, &quot;Natan&quot;, &quot;Ede&quot;)\r\n\r\nfriends\r\n## [1] &quot;Anna&quot;  &quot;Otto&quot;  &quot;Natan&quot; &quot;Ede&quot;\r\n<\/pre>\n<p>4) Index the second element from the vector <span class=\"crayon-inline\">friends<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nfriends[2]\r\n## [1] \"Otto\"\r\n<\/pre>\n<p>5) Replace the first element of the vector <span class=\"crayon-inline\">friends<\/span> with &#8220;Isolde&#8221; and check the updated vector again!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nfreunde[1] &lt;- &quot;Isolde&quot;\r\n\r\nfreunde\r\n## [1] &quot;Isolde&quot; &quot;Otto&quot;   &quot;Natan&quot;  &quot;Ede&quot;\r\n<\/pre>\n<p>6) Create a vector <span class=\"crayon-inline\">v1<\/span> from the following elements <span class=\"crayon-inline\">&nbsp;1, &#8220;Hello&#8221;, 2, &#8220;World&#8221;&nbsp;<\/span>! Check the object class!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nv1 &lt;- c(1, &quot;Hello&quot;, 2, &quot;World&quot;)\r\n\r\nv1\r\n## [1] &quot;1&quot;     &quot;Hello&quot; &quot;2&quot;     &quot;World&quot;\r\n\r\nclass(v1)\r\n## [1] &quot;character&quot;\r\n<\/pre>\n<p>7) Create a vector v2 with numerical values \u200b\u200b(only integers) ranging from 4 to 10!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nv2 &lt;- c(4, 5, 6, 7, 8, 9, 10)\r\n\r\nv2\r\n## [1]  4  5  6  7  8  9 10\r\n\r\n# or use the sequence operator &quot;:&quot;\r\n\r\nv2 &lt;- c(4:10)\r\n\r\nv2\r\n## [1]  4  5  6  7  8  9 10\r\n<\/pre>\n<p>8) Index the first three elements from <span class=\"crayon-inline\">v2<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nv2[1:3]\r\n## [1] 4 5 6\r\n\r\n# or:\r\n\r\nv2[ c(1, 2, 3) ]\r\n## [1] 4 5 6\r\n<\/pre>\n<p>9) Index all elements of <span class=\"crayon-inline\">v2<\/span> except the second element and save the result as <span class=\"crayon-inline\">v2.subset<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nv2.subset &lt;- v2[-2]\r\n\r\nv2.subset\r\n## [1]  4  6  7  8  9 10\r\n<\/pre>\n<p>10) Use the length () function to find out the length of <span class=\"crayon-inline\">v2.subset<\/span> (= the number of elements in the vector)!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nlength(v2.subset)\r\n## [1] 6\r\n<\/pre>\n<p>11) Calculate the arithmetic mean of vector <span class=\"crayon-inline\">v2<\/span>! In addition, determine the standard deviation of <span class=\"crayon-inline\">v2.subset<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nmean(v2)\r\n## [1] 7\r\n\r\nsd(v2.subset)\r\n## [1] 2.160247\r\n<\/pre>\n<hr style=\"height: 4px;background-color: #6b9e1f\" \/>\n<a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/r-crash-course-3\/\"><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 R Crash Course<\/em><\/strong><\/span>\n<\/div>\n<p><\/button><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1) Create an integer variable e holding the value 42! Check the object class of e with class()! e &lt;- 42L class(e) ## [1] &quot;integer&quot; 2) Convert e to the character data type with as.character()! Check the object class again! e &lt;- as.character(e) class(e) ## [1] &quot;character&quot; 3) Create a character vector friends with four &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/e02\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;R &#8211; Exercise II&#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-793","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/793","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=793"}],"version-history":[{"count":10,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/793\/revisions"}],"predecessor-version":[{"id":2470,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/793\/revisions\/2470"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}