{"id":824,"date":"2018-05-19T18:55:45","date_gmt":"2018-05-19T16:55:45","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=824"},"modified":"2018-09-25T10:32:25","modified_gmt":"2018-09-25T08:32:25","slug":"e04","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/e04\/","title":{"rendered":"R &#8211; Exercise IV"},"content":{"rendered":"<p>1) Create a data frame <span class=\"crayon-inline\">df<\/span> that contains the following variables for at least four observations:<\/p>\n<ul>\n<li>name: name of at least four friends or acquaintances<\/li>\n<li>age: the age of those persons<\/li>\n<li>size: the height of those persons in cm<\/li>\n<li>city: Place of residence of those persons (city)<\/li>\n<\/ul>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\ndf &lt;- data.frame(\r\n  name = c(&quot;Anna&quot;, &quot;Otto&quot;, &quot;Natan&quot;, &quot;Ede&quot;), \r\n  age  = c(66, 53, 22, 36),\r\n  size = c(170, 174, 182, 180),\r\n  city = c(&quot;Hamburg&quot;, &quot;Berlin&quot;, &quot;Berlin&quot;, &quot;Cologne&quot;)\r\n  )\r\n\r\ndf\r\n##    name age size    city\r\n## 1  Anna  66  170 Hamburg\r\n## 2  Otto  53  174  Berlin\r\n## 3 Natan  22  182  Berlin\r\n## 4   Ede  36  180 Cologne\r\n<\/pre>\n<p>2) Examine the dimensionality, structure and statistical summary of your data frame <span class=\"crayon-inline\">df<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\ndim(df)\r\n## [1] 4 4\r\n\r\nstr(df)\r\n## 'data.frame':    4 obs. of  4 variables:\r\n##  $ name: Factor w\/ 4 levels \"Anna\",\"Ede\",\"Natan\",..: 1 4 3 2\r\n##  $ age : num  66 53 22 36\r\n##  $ size: num  170 174 182 180\r\n##  $ city: Factor w\/ 3 levels \"Berlin\",\"Cologne\",..: 3 1 1 2\r\n\r\nsummary(df)\r\n##     name        age             size            city  \r\n##  Anna :1   Min.   :22.00   Min.   :170.0   Berlin :2  \r\n##  Ede  :1   1st Qu.:32.50   1st Qu.:173.0   Cologne:1  \r\n##  Natan:1   Median :44.50   Median :177.0   Hamburg:1  \r\n##  Otto :1   Mean   :44.25   Mean   :176.5              \r\n##            3rd Qu.:56.25   3rd Qu.:180.5              \r\n##            Max.   :66.00   Max.   :182.0\r\n<\/pre>\n<p>3) Index the second column with simple square brackets <span class=\"crayon-inline\">[]<\/span> and save the output as <span class=\"crayon-inline\">df.subset<\/span>! Which class does the output belong to?<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\ndf.subset &lt;- df[2]\r\n\r\ndf.subset\r\n##   age\r\n## 1  66\r\n## 2  53\r\n## 3  22\r\n## 4  36\r\n\r\nclass(df.subset)\r\n## [1] &quot;data.frame&quot;\r\n<\/pre>\n<p>4) Index the variable <span class=\"crayon-inline\">age<\/span> with double square brackets <span class=\"crayon-inline\">[[]]<\/span> and save the output as <span class=\"crayon-inline\">age.persons<\/span>. Which class does the output belong to?<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nage.persons &lt;- df[[&quot;alter&quot;]]\r\n\r\n# oder\r\n\r\nage.persons &lt;- df[[2]]\r\n\r\nage.persons\r\n## [1] 66 53 22 36\r\n\r\nclass(age.persons)\r\n## [1] &quot;numeric&quot;\r\n<\/pre>\n<p>5) Add the variable <span class=\"crayon-inline\">weight<\/span> in kg to the data frame <span class=\"crayon-inline\">df<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\ndf$weight &lt;- c(115, 110.2, 95, 87)\r\n\r\ndf\r\n##    name age size    city weight\r\n## 1  Anna  66  170 Hamburg  115.0\r\n## 2  Otto  53  174  Berlin  110.2\r\n## 3 Natan  22  182  Berlin   95.0\r\n## 4   Ede  36  180 Cologne   87.0\r\n<\/pre>\n<p>6) Add another observation (person) to your <span class=\"crayon-inline\">df<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nnew.person &lt;- data.frame(\r\n  &quot;name&quot;   = &quot;Anna&quot;,\r\n  &quot;age&quot;    = 32,\r\n  &quot;size&quot;   = 174,\r\n  &quot;weight&quot; = 63,\r\n  &quot;city&quot;   = &quot;Hamburg&quot;\r\n)\r\n\r\ndf &lt;- rbind(df, new.person)\r\n\r\ndf\r\n##    name age size    city weight\r\n## 1  Anna  66  170 Hamburg  115.0\r\n## 2  Otto  53  174  Berlin  110.2\r\n## 3 Natan  22  182  Berlin   95.0\r\n## 4   Ede  36  180 Cologne   87.0\r\n## 5  Anna  32  174 Hamburg   63.0\r\n<\/pre>\n<p>7) Calculate the mean value of the variable <span class=\"crayon-inline\">age<\/span> and save the result as <span class=\"crayon-inline\">ages.mean<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\nages.mean &lt;- mean(df$age)\r\n<\/pre>\n<p>8) Index all observations (persons) that are older than the average <span class=\"crayon-inline\">ages.mean<\/span>!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\ndf[df$age &gt; ages.mean, ]\r\n<\/pre>\n<p>9) Index all persons, which are lighter than 100 kg AND at least 180 cm tall!<\/p>\n<pre class=\"theme:amityreseda minimize:true\" title=\"show code\">\r\ndf[df$weight = 180, ]\r\n##    name age size    city weight\r\n## 3 Natan  22  182  Berlin     95\r\n## 4   Ede  36  180 Cologne     87\r\n\r\n# or\r\n\r\nsubset(df, df$weight = 180)\r\n##    name age size    city weight\r\n## 3 Natan  22  182  Berlin     95\r\n## 4   Ede  36  180 Cologne     87\r\n<\/pre>\n<hr style=\"height: 4px;background-color: #6b9e1f\" \/>\n<a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/r-crash-course-5\/\"><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 a data frame df that contains the following variables for at least four observations: name: name of at least four friends or acquaintances age: the age of those persons size: the height of those persons in cm city: Place of residence of those persons (city) df &lt;- data.frame( name = c(&quot;Anna&quot;, &quot;Otto&quot;, &quot;Natan&quot;, &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/e04\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;R &#8211; Exercise IV&#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-824","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/824","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=824"}],"version-history":[{"count":9,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/824\/revisions"}],"predecessor-version":[{"id":2475,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/824\/revisions\/2475"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}