{"id":829,"date":"2018-05-19T19:27:38","date_gmt":"2018-05-19T17:27:38","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=829"},"modified":"2018-09-25T10:33:52","modified_gmt":"2018-09-25T08:33:52","slug":"e05","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/e05\/","title":{"rendered":"R &#8211; Exercise V"},"content":{"rendered":"<p>1) Use <span class=\"crayon-inline\">ifelse()<\/span> and your data frame <span class=\"crayon-inline\">df<\/span> from exercise IV: If the person is less than or equal to 175 cm, it should have the attribute &#8220;small&#8221;, otherwise &#8220;tall&#8221;. Save the result in your <span class=\"crayon-inline\">df<\/span> as the new column <span class=\"crayon-inline\">size.category<\/span>.<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nx &lt;- ifelse(df$size &lt;= 175, &quot;small&quot;, &quot;tall&quot;)\r\n\r\nx\r\n## [1] &quot;small&quot; &quot;small&quot; &quot;tall&quot; &quot;tall&quot; &quot;small&quot;\r\n\r\ndf$size.categorie &lt;- x\r\n\r\ndf\r\n##    name age size    city weight size.categorie\r\n## 1  Anna  66  170 Hamburg  115.0          small\r\n## 2  Otto  53  174  Berlin  110.2          small\r\n## 3 Natan  22  182  Berlin   95.0          tall\r\n## 4   Ede  36  180 Cologne   87.0          tall\r\n## 5  Anna  32  174 Hamburg   63.0          small\r\n<\/pre>\n<p>2) Write a loop that outputs all integers from 5 to 15!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nvektor &lt;- 5:15\r\nvektor\r\n##  [1]  5  6  7  8  9 10 11 12 13 14 15\r\n\r\nfor (i in vektor) {\r\n  print(i)\r\n}\r\n## [1] 5\r\n## [1] 6\r\n## [1] 7\r\n## [1] 8\r\n## [1] 9\r\n## [1] 10\r\n## [1] 11\r\n## [1] 12\r\n## [1] 13\r\n## [1] 14\r\n## [1] 15\r\n<\/pre>\n<p>3) Advanced: Create a for loop that outputs the arithmetic mean for each variable (column) of your data frame <span class=\"crayon-inline\">df<\/span> &#8211; provided that the variable is numeric!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nfor (i in 1:ncol(df)) {\r\n  \r\n  if (class(df[[i]]) == \"numeric\") {\r\n    print(names(df)[i])\r\n    result &lt;- mean(df[[i]], na.rm=TRUE)\r\n    print(result)\r\n  }\r\n  \r\n}\r\n## [1] &quot;age&quot;\r\n## [1] 41.8\r\n## [1] &quot;size&quot;\r\n## [1] 176\r\n## [1] &quot;weight&quot;\r\n## [1] 94.04\r\n\r\n# Even if it looks complicated, take your time and go through it line by line. Everything should be known by now! \r\n<\/pre>\n<hr style=\"height: 4px;background-color: #6b9e1f\" \/>\n<a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/acquire\/\"><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>Acquire<\/em><\/strong><\/span>\n<\/div>\n<p><\/button><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1) Use ifelse() and your data frame df from exercise IV: If the person is less than or equal to 175 cm, it should have the attribute &#8220;small&#8221;, otherwise &#8220;tall&#8221;. Save the result in your df as the new column size.category. x &lt;- ifelse(df$size &lt;= 175, &quot;small&quot;, &quot;tall&quot;) x ## [1] &quot;small&quot; &quot;small&quot; &quot;tall&quot; &quot;tall&quot; &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/e05\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;R &#8211; Exercise V&#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-829","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/829","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=829"}],"version-history":[{"count":10,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/829\/revisions"}],"predecessor-version":[{"id":2477,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/829\/revisions\/2477"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}