{"id":348,"date":"2018-05-03T14:33:38","date_gmt":"2018-05-03T12:33:38","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=348"},"modified":"2018-10-15T15:31:36","modified_gmt":"2018-10-15T13:31:36","slug":"r-crash-course","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/r-crash-course\/","title":{"rendered":"R Crash Course Part I"},"content":{"rendered":"<p><a name=\"ch1\"><\/a><\/p>\n<h1>1. Package Management<\/h1>\n<p>Packages are collections of functions and compiled code written by the R community or the <a href=\"https:\/\/www.r-project.org\/contributors.html\">R Development Core Team<\/a>. R comes with a standard set of packages, e.g., <em>base<\/em>, <em>stats<\/em>, and <em>graphics<\/em>. However, R is enormously expandable in its (geo-)statistical functionality via the official network CRAN (The Comprehensive R Archive Network). There are over 11,000 different extensions, or packages, available free of charge. The directory, where installed packages are stored, is called library.<br \/>\nYour can install new packages to your library by clicking on the <strong>Packages<\/strong>-tab and then <strong>Install<\/strong> in the Files Pane of R-Studio. Anyway, download and installation of packages can also be done script-based via the function <span class=\"crayon-inline\">install.packages()<\/span> (illustrated using the <a href=\"https:\/\/cran.r-project.org\/web\/packages\/ggplot2\/ggplot2.pdf\">ggplot2<\/a> package, which is useful for creating elegant data visualisations):<\/p>\n<pre class=\"r\"><code>install.packages(\"ggplot2\")\r\n\r\nlibrary()<\/code><\/pre>\n<p>The function <span class=\"crayon-inline\">library()<\/span> in line 3 lists all currently installed packages on your system. This list is also visible in the Files Pane of R-Studio (Packages tab). If problems arise, make sure VirtualBox has access to the internet and is not blocked by a firewall on your host system. Keep in mind that packages must be installed only once and remain permanently installed, even after a restart of R-Studio.<br \/>\nOnce installed, you have to load the package into your current R-session before you can use its functionalities by using <span class=\"crayon-inline\">library()<\/span> together with the package name:<\/p>\n<pre class=\"theme:amityreseda\">\r\nlibrary(ggplot2)    \r\n<\/pre>\n<p>You can list all packages, wich are loaded in your current R session by using <span class=\"crayon-inline\">search()<\/span>. Activated packages also have a tick symbol next to their names in the Files pane (Packages tab) of R-Studio.<\/p>\n<p><a name=\"ch2\"><\/br><\/a><\/p>\n<h1>2. Calculate With R<\/h1>\n<p>Of course, R can be used as a simple calculator. Required operators can be entered directly into the Console pane of R-Studio or as a whole script in the Source Pane, from which you can send the commands to the Console pane with <strong>Ctrl+Enter<\/strong>. Results are then immediately printed to the console. In this online course, corresponding outputs are also shown with two hash tags at the beginning of the line for better transparency and readability:<\/p>\n<pre class=\"theme:amityreseda\">\r\n# hash tags allow you to make valuable notes and reminders \r\n\r\n19 + 23      \r\n## [1] 42\r\n\r\n34 - 22     \r\n## [1] 12\r\n\r\n27 \/ 9       \r\n## [1] 3\r\n\r\n6 * 8        \r\n## [1] 48\r\n\r\n(2 + 3) * 4  \r\n## [1] 20\r\n<\/pre>\n<p>You will see a number in square brackets <span class=\"crayon-inline\">[1]<\/span> at the beginning of your output prompts. This number refers to the length of your output, i.e., the number of elements, which is 1 for all examples above. More on that in chapter <a href=\"#ch5\">Vectors<\/a>. In addition to these standard operators, there were plenty of other operators commonly encountered in R: <\/p>\n<table style='border:none'>\n<tr>\n<td width=\"240\" style='border:none;padding:0;text-align:center'><span class=\"crayon-inline theme:amityresedaterminal\">&nbsp; ? &nbsp;<\/span><\/td>\n<td style='border:none;padding:0'>help function<\/td>\n<\/tr>\n<tr>\n<td style='border:none;padding:0;text-align:center'><span class=\"crayon-inline theme:amityresedaterminal\">&nbsp; + &nbsp; &#8211; &nbsp; \/ &nbsp; * &nbsp; ^ &nbsp;<\/span><\/td>\n<td style='border:none;padding:0'>addition, subtraction, division, multiplication, potentiation<\/td>\n<\/tr>\n<tr>\n<td style='border:none;padding:0;text-align:center'><span class=\"crayon-inline theme:amityresedaterminal\">&nbsp; ! &nbsp;<\/span><\/td>\n<td style='border-right:none;border-left:none;border-bottom:none;border-top:none;padding:0 0 0 0'>negation sign<\/td>\n<\/tr>\n<tr>\n<td style='border:none;padding:0;text-align:center'><span class=\"crayon-inline theme:amityresedaterminal\">&nbsp; &lt; &nbsp; &gt; &nbsp; &lt;= &nbsp; &gt;= &nbsp; == &nbsp; != &nbsp;<\/span><\/td>\n<td style='border:none;padding:0'>lesser, greater, lesser or equal, greater or equal, equal, not equal<\/td>\n<\/tr>\n<tr>\n<td style='border:none;padding:0;text-align:center'><span class=\"crayon-inline theme:amityresedaterminal\">&nbsp; &amp; &nbsp; | &nbsp;<\/span><\/td>\n<td style='border:none;padding:0'>boolean AND, boolean OR<\/td>\n<\/tr>\n<tr>\n<td style='border:none;padding:0;text-align:center'><span class=\"crayon-inline theme:amityresedaterminal\">&nbsp; &lt;- &nbsp;<\/span><\/td>\n<td style='border:none;padding:0'>variable assignment<\/td>\n<\/tr>\n<tr>\n<td style='border:none;padding:0;text-align:center'><span class=\"crayon-inline theme:amityresedaterminal\">&nbsp; ~ &nbsp;<\/span><\/td>\n<td style='border:none;padding:0'>separate left- and right-hand sides in a model formula<\/td>\n<\/tr>\n<tr>\n<td style='border:none;padding:0;text-align:center'><span class=\"crayon-inline theme:amityresedaterminal\">&nbsp; : &nbsp;<\/span><\/td>\n<td style='border:none;padding:0'>generate regular sequences<\/td>\n<\/tr>\n<tr>\n<td style='border:none;padding:0;text-align:center'><span class=\"crayon-inline theme:amityresedaterminal\">&nbsp; %% &nbsp;<\/span><\/td>\n<td style='border:none;padding:0'>modulo<\/td>\n<\/tr>\n<tr>\n<td style='border:none;padding:0;text-align:center'><span class=\"crayon-inline theme:amityresedaterminal\">&nbsp; [] &nbsp;[[]]&nbsp; $ &nbsp;@ &nbsp;<\/span><\/td>\n<td style='border:none;padding:0'>indexing in vectors, matrices, and data frames<\/td>\n<\/tr>\n<\/table>\n<p>The help operator in R provides access to the documentation pages for R functions, data sets, and other objects, both for packages in the standard R distribution and for contributed packages. In order to access documentation for the sequence operator, for example, enter the command <span class=\"crayon-inline\">?&#8221;:&#8221;<\/span> or <span class=\"crayon-inline\">help(&#8220;:&#8221;)<\/span>.<\/p>\n<p><a name=\"ch3\"><\/a><\/br><\/p>\n<h1>3. Variables<\/h1>\n<p>In most cases, however, you will want to cache results of commands in order re-access them later on. Then, variables come into play. In R variables are defined using the <span class=\"crayon-inline\">&nbsp;&lt;-&nbsp;<\/span> operator. Although the output will not be printed to the Console pane directly, we store the variable in our temporary workspace. Thus, the variable should be visible under Values \u200b\u200bin the Environment pane in R-Studio.<\/p>\n<pre class=\"theme:amityreseda\">\r\nx &lt;- 8 + 7         # assignment to variable x\r\n\r\ny &lt;- 4 * 2         # assignment to variable y\r\n<\/pre>\n<p>We only get the value of the variable when we call its name as a command or look into the Environment pane. Further calculations with the variables are also possible:<\/p>\n<pre class=\"theme:amityreseda\">\r\nx            \r\n## [1] 15\r\n\r\ny  \r\n## [1] 8\r\n\r\nx + y        \r\n## [1] 23\r\n\r\nmy.variable &lt;- x + y\r\nmy.variable \r\n## [1] 23\r\n<\/pre>\n<p>A convention in R is to include points in variable names, e.g., <span class=\"crayon-inline\">my.variable<\/span>. This is for the sake of clarity only, especially when many variables exist, and has no deeper meaning beyond that. Strictly avoid any other special symbols in variable names.<\/p>\n<p><a name=\"ch4\"><\/a><\/br><\/p>\n<h1>4. Functions<\/h1>\n<p>A function is a piece of code written to carry out a specific task. It can accept arguments and returns one or more values.<br \/>\nR standard packages offer several arithmetic built-in functions and constants, which make statistical analysis quite efficient. A function generally consists of a function name and two parentheses <span class=\"crayon-inline\">&nbsp;()&nbsp;<\/span>, in which arguments are given as input. Of course, previously created variables can serve as arguments for functions, too:<\/p>\n<pre class=\"theme:amityreseda\">\r\nsqrt(64)           # square root \r\n## [1] 8\r\n\r\nexp(3)             # exponential\r\n## [1] 20.08554\r\n\r\ncos(13)            # cosinus\r\n## [1] 0.9074468\r\n\r\npi                 # constant number pi\r\n## [1] 3.141593\r\n\r\nround(pi)          # round values\r\n## [1] 3\r\n\r\na &lt;- 6\r\nb &lt;- 9.2\r\nlog10(a + b)       # logarithm (base 10)\r\n## [1] 1.181844\r\n<\/pre>\n<p>So, the best way to learn about the internal workings of a function is to write your own one. R allows to create user defined functions, whereby the basic construct of each function is the following:<\/p>\n<pre class=\"theme:amityreseda\">\r\nname.of.fun( arguments ) { body }\r\n<\/pre>\n<p>The code in between the curly braces is the body of the function. This is where you define all the commands your functions will perform. Let us write a function that calculates a normalized ratio of two numeric values! It is best to copy the following code into the script window, select everything and then execute the code. The function should then appear in the Environment window and can be called hereinafter.<\/p>\n<pre class=\"theme:amityreseda\">\r\nmy.fun &lt;- function(var1, var2) {\r\n  x &lt;- (var1 - var2) \/ (var1 + var2)\r\n  return(x)\r\n}\r\n<\/pre>\n<p>Explanation: Use <span class=\"crayon-inline\">function()<\/span> to create a new function and assign it to any variable, e.g.,<span class=\"crayon-inline\">my.fun<\/span>. The two arguments <span class=\"crayon-inline\">var1<\/span> and <span class=\"crayon-inline\">var2<\/span> are placeholders for variables that are assigned when this function is called. Operations of the function are defined between the curly braces <span class=\"crayon-inline\">{}<\/span>. Intermediate results, i.e., the <span class=\"crayon-inline\">x<\/span> in our example, exist locally within the function &#8211; they do not appear in the Environment window. Only variables given to the <span class=\"crayon-inline\">return<\/span> function can be saved as a variable. The function call is done via:<\/p>\n<pre class=\"theme:amityreseda\">\r\nresult &lt;- my.fun(42, 13)\r\nresult \r\n## [1] 0.5272727\r\n<\/pre>\n<p>If you feel ready click the button below and check your understanding up to here!<\/p>\n<p><a target=\"_blank\" href=\"https:\/\/blogs.fu-berlin.de\/reseda\/e01\/\"><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 I<\/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\/r-crash-course-2\/\"><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>R Crash Course Part II<\/em><\/strong><\/span>\n<\/div>\n<p><\/button><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Package Management Packages are collections of functions and compiled code written by the R community or the R Development Core Team. R comes with a standard set of packages, e.g., base, stats, and graphics. However, R is enormously expandable in its (geo-)statistical functionality via the official network CRAN (The Comprehensive R Archive Network). There &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/r-crash-course\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;R Crash Course Part I&#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-348","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/348","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=348"}],"version-history":[{"count":122,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/348\/revisions"}],"predecessor-version":[{"id":2859,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/348\/revisions\/2859"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}