{"id":1460,"date":"2018-06-21T23:50:31","date_gmt":"2018-06-21T21:50:31","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=1460"},"modified":"2018-09-25T10:33:29","modified_gmt":"2018-09-25T08:33:29","slug":"r-crash-course-5","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/r-crash-course-5\/","title":{"rendered":"R Crash Course Part V"},"content":{"rendered":"<p><a name=\"ch9\"><\/a><\/p>\n<h1>9. Missing values<\/h1>\n<p>Sometimes observations (rows) in a data frame are incomplete. The correct handling of missing values is especially needed in statistical evaluations. Missing values are indicated by the symbol <span class=\"crayon-inline\">NA<\/span> (Not Available). Non-possible values (e.g., division by 0) are described by the symbol <span class=\"crayon-inline\">NaN<\/span> (Not a Number). For test purposes, we artificially define two elements as <span class=\"crayon-inline\">NA<\/span> in our <span class=\"crayon-inline\">df<\/span>:<\/p>\n<pre class=\"theme:amityreseda\">\r\ndf &lt;- data.frame(\r\n  &quot;name&quot; = c(&quot;Ben&quot;, &quot;Hanna&quot;, &quot;Paul&quot;, &quot;Arthur&quot;), \r\n  &quot;size&quot; = c(185, 166, 175, 190)\r\n  )\r\n\r\ndf\r\n##     name size\r\n## 1    Ben  185\r\n## 2  Hanna  166\r\n## 3   Paul  175\r\n## 4 Arthur  190\r\n\r\ndf[1:2, 2] &lt;- NA              # generate NAs\r\ndf\r\n##     name size\r\n## 1    Ben   NA\r\n## 2  Hanna   NA\r\n## 3   Paul  175\r\n## 4 Arthur  190\r\n\r\nmean(df$size)                 # mean with NAs\r\n## [1] NA\r\n\r\nmean(df$size, na.rm=TRUE)     # mean with ignoring NAs\r\n## [1] 182.5\r\n<\/pre>\n<p>Often it makes more sense to record the observations with missing values and to exclude them from your dataset. For detection we can use the <span class=\"crayon-inline\">is.na()<\/span> function to create a logical vector. This outputs a TRUE for every NA value, a FALSE for every element present. Using the logical vector we can then index our data frame:<\/p>\n<pre class=\"theme:amityreseda\">\r\nis.na(df$size)               # logical vector (TRUE for all rows with NAs)\r\n## [1]  TRUE  TRUE FALSE FALSE\r\n\r\ndf[is.na(df$size), ]         # index using logical vector\r\n##    name size\r\n## 1   Ben   NA\r\n## 2 Hanna   NA\r\n\r\ndf[complete.cases(df), ]     # get all rows without NAs\r\n##     name size\r\n## 3   Paul  175\r\n## 4 Arthur  190\r\n<\/pre>\n<p><a name=\"ch10\"><\/a><\/br><\/p>\n<h1>10. Control Structures<\/h1>\n<p>Control structures allow better control over the execution of our scripts, e.g., <span class=\"crayon-inline\">if<\/span>, <span class=\"crayon-inline\">if else<\/span>, <span class=\"crayon-inline\">else<\/span>, <span class=\"crayon-inline\">while<\/span>, <span class=\"crayon-inline\">switch<\/span>, <span class=\"crayon-inline\">repeat<\/span>, <span class=\"crayon-inline\">break<\/span>, <span class=\"crayon-inline\">return<\/span> &#8230;<br \/>\nUse the help function for a more detailed documentation. <\/p>\n<p><strong>The IF statement<\/strong><\/p>\n<p>Use the if \/ else command to perform simple queries on all data types. The Microsoft Excel equivalent would be the &#8220;IF&#8221; feature. In R, the syntax is as follows:<\/p>\n<pre class=\"theme:amityreseda\">\r\nx &lt;- 3                                # define x\r\n\r\nx &lt;= 4                                # logical condition\r\n## [1] TRUE\r\n\r\nif (x &lt;= 4) {\r\n  print(&quot;x is smaller than or equal to 4!&quot;)    \r\n} else {\r\n  print(&quot;x is larger than 4!&quot;)        \r\n}\r\n## [1] &quot;x is smaller than or equal to 4!&quot;\r\n<\/pre>\n<p>Explanation: An IF-command needs three parts: the keyword <span class=\"crayon-inline\">if()<\/span>, a condition that results in a single logical output <span class=\"crayon-inline\">x &lt;= 4<\/span> and a block of code in curly braces <span class=\"crayon-inline\">{}<\/span>, which is executed if the expression is TRUE. So, if the condition is TRUE, the code will run in curly brackets after the IF-command. If the condition is FALSE, the code block after the <span class=\"crayon-inline\">else<\/span>is executed.<br \/>\nThe <span class=\"crayon-inline\">print()<\/span> function outputs the strings in the parentheses to the console window. Here we need the <span class=\"crayon-inline\">print()<\/span> function so that the output can be written out of the IF-function, similar to <span class=\"crayon-inline\">return<\/span>(see <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/r-crash-course\/#ch5\">chapter 5<\/a>). A vectorized (and therefore more efficient) notation is the following:<\/p>\n<pre class=\"theme:amityreseda\">\r\nx &lt;- c(3, 4, 5, 6, 7)                                \r\n\r\nifelse(x &lt;=4, &quot;x is smaller than or equal to 4!&quot;, &quot;x is larger than 4!&quot;)\r\n## [1] &quot;x is smaller than or equal to 4!&quot; &quot;x is smaller than or equal to 4!&quot;\r\n## [3] &quot;x is larger than 4!&quot;              &quot;x is larger than 4!&quot;             \r\n## [5] &quot;x is larger than 4!&quot;\r\n<\/pre>\n<p>Using <span class=\"crayon-inline\">ifelse()<\/span>, the condition for each individual element is determined as a vector. This is helpful, e.g., if we want to categorize data!<\/p>\n<p><strong>The FOR loop<\/strong><\/p>\n<p>Loops are incredibly useful when certain tasks need to be repeated very often in the script. A for loop is based on an iterable variable of defined length. But what does that mean? We define any variable, e.g., <span class=\"crayon-inline\">i<\/span>, with a start integer value, e.g, 1. We then increment this integer value until a second integer value, e.g. 8, is reached. This can be done via the sequence operator <span class=\"crayon-inline\">:<\/span>:<\/p>\n<pre class=\"theme:amityreseda\">\r\nfor (i in 1:8) {\r\n  print(i)\r\n}\r\n## [1] 1\r\n## [1] 2\r\n## [1] 3\r\n## [1] 4\r\n## [1] 5\r\n## [1] 6\r\n## [1] 7\r\n## [1] 8\r\n<\/pre>\n<p>The practical thing: The code in curly brackets is automatically executed once each time (eight times in total)! And we can meanwhile pick up the expression of our variable <span class=\"crayon-inline\">i<\/span>, in order to print it or index a vector with it and much more:<\/p>\n<pre class=\"theme:amityreseda\">\r\nv &lt;- c(23, 54, 12, 59, 67, 45)    # create integer vector\r\n\r\nlength(v)                         # check length of vector\r\n## [1] 6\r\n  \r\nfor (i in 1:length(v)) {          # iterate length(v) times\r\n  print(v[i])\r\n}\r\n## [1] 23\r\n## [1] 54\r\n## [1] 12\r\n## [1] 59\r\n## [1] 67\r\n## [1] 45\r\n<\/pre>\n<p>It is also possible to set the variable \/ iterator equal to the elements of the vector instead of an integer value for indexing. The information in which run the loop is, however, is initially lost:<\/p>\n<pre class=\"theme:amityreseda\">\r\nv &lt;- c(&quot;R&quot;, &quot;is&quot;, &quot;still&quot;, &quot;fun&quot;)    \r\n\r\nfor (i in v) {\r\n  print(i)\r\n}\r\n## [1] &quot;R&quot;\r\n## [1] &quot;is&quot;\r\n## [1] &quot;still&quot;\r\n## [1] &quot;fun&quot;\r\n<\/pre>\n<p>Now, have a look at exercise V:<\/p>\n<p><a target=\"_blank\" href=\"https:\/\/blogs.fu-berlin.de\/reseda\/e05\/\"><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 V<\/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\/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: 12px;color:#bfbfbf\"><strong><em>NEXT<\/em><\/strong><\/span><br \/>\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>9. Missing values Sometimes observations (rows) in a data frame are incomplete. The correct handling of missing values is especially needed in statistical evaluations. Missing values are indicated by the symbol NA (Not Available). Non-possible values (e.g., division by 0) are described by the symbol NaN (Not a Number). For test purposes, we artificially define &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/r-crash-course-5\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;R Crash Course Part 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-1460","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1460","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=1460"}],"version-history":[{"count":4,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1460\/revisions"}],"predecessor-version":[{"id":2476,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1460\/revisions\/2476"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=1460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}