{"id":2489,"date":"2014-11-10T21:33:36","date_gmt":"2014-11-10T21:33:36","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2489"},"modified":"2014-11-11T10:27:41","modified_gmt":"2014-11-11T10:27:41","slug":"starting-out-with-r","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/starting-out-with-r\/","title":{"rendered":"Starting out with R"},"content":{"rendered":"<p>This will be a simple &#8220;getting started&#8221; post on the R environment and language. <a href=\"http:\/\/en.wikipedia.org\/wiki\/R_(programming_language)\" title=\"R (Programming Language)\" target=\"_blank\">R<\/a> is a statistical language which is also heavily used in data mining and machine learning.<\/p>\n<p>For this post I&#8217;m using R version 3.1.2 and the free version of <a href=\"http:\/\/www.rstudio.com\/products\/RStudio\/#Desk\" title=\"R Studio\" target=\"_blank\">R Studio<\/a> 0.98.1087. Assuming we have R installed and then R Studio, let&#8217;s get started&#8230;<\/p>\n<p><strong>Demos<\/strong><\/p>\n<p>Let&#8217;s start by looking at the <em>demo<\/em> command. Typing <em>demo()<\/em> within the console of R Studio will list the available demos within the packages installed. So if we install further packages which contain demos, these will be available when you invoke the <em>demo<\/em> command.<\/p>\n<p>So <em>demo<\/em> will list the demos within the packages, we can invoke <\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\ndemo(package='graphics')\r\n<\/pre>\n<p>to get the graphics package demos and to run a demo simply type<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\ndemo(image)\r\n<\/pre>\n<p>for example to run the image demo.<\/p>\n<p>To find a list of all demos within all packages we can invoke<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\ndemo(package = .packages(all.available=TRUE))\r\n<\/pre>\n<p><strong>R scripts<\/strong><\/p>\n<p>By default we can execute commands in the R console, but ofcourse we might wish to put a whole bunch of commands together into a script file, so within R Studio we can use Ctrl+Shift+N to create a new script file and then obviously enter the script. We can then run the script using Ctrl+Shift+S (or Code | Source).<\/p>\n<p><strong>Help<\/strong><\/p>\n<p>The help function can be invoked from the console or R Studio to view help on a function\/command, (this will load an HTML page, in the case of R Studio this is displayed within the IDE)<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# display the help documentation\r\nhelp() \r\n\r\n# display the help documentation for the vector type\r\nhelp(vector)\r\n<\/pre>\n<p>We can also use the shortcut ? to invoke the help<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n?vector\r\n<\/pre>\n<p><strong>Vignette<\/strong><\/p>\n<p>Vignettes are documents (in PDF format) which are available as help for packages. <\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nvignette()\r\n<\/pre>\n<p>the above will, like <em>demo()<\/em> list all vignettes in the packages. <\/p>\n<p>Again we can list all vignettes across all packages using<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nvignette(package = .packages(all.available = TRUE))\r\n<\/pre>\n<p>and you guessed it we can equally run the following to list the vignettes within a specific package<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nvignette(package = 'grid')\r\n<\/pre>\n<p>Finally to invoke a vignette we simple execute<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nvignette('grid')\r\n<\/pre>\n<p>to execute a specific vignette on a specific package we can equally write<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nvignette('moveline', package='grid')\r\n<\/pre>\n<p><strong>Double or single quote symbols?<\/strong><\/p>\n<p>We can use either double or single quotes, interchangeably within R but obviously if we want to embed a single or double quote within a string, we can use the other type of quote, for example<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\ns &lt;- 'This is a &quot;double quote&quot; string'\r\n<\/pre>\n<p>As per C# we can equally use the backslash to escape the quote<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\ns &lt;- &quot;This is a \\&quot;double quote\\&quot; string&quot;\r\n<\/pre>\n<p><strong>Comments<\/strong><\/p>\n<p>Before we start writing code, let&#8217;s look at how to write comments within R. The # is used to prefix a comment, for example<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# this is a comment\r\n<\/pre>\n<p><strong>Variables<\/strong><\/p>\n<p>Like most programming languages, we want ways to store data, like F# we use the <- operator to assign values to a variable, i.e.\n\n[code language=\"R\"]\nx &lt;- 123\n[\/code]\n\nso obviously in the above x is the variable name and is now assigned the value 123.\n\nWe can also use the assign variables using the assign function, for example\n\n[code language=\"R\"]\nassign(&quot;x&quot;, 123)\n[\/code]\n\nnotice the variable name is written as a string.\n\nWe can also use -> and = to assign variables<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n123 -&gt; x\r\n\r\nx = 123\r\n<\/pre>\n<p>however the equals operator is rarely used from what I can tell, partly because it can have different meanings in different situations.<\/p>\n<p>Variable names in R can be made up of letters, numbers, dot and underline characters. Variables must start with a letter or dot (note: a dot cannot be followed by a number as a variable name). Ofcourse like other languages, R keywords cannot be used for variable names.<\/p>\n<p>If you&#8217;re interested in R style guidelines which include guidelines for variable naming styles, checkout <a href=\"https:\/\/google-styleguide.googlecode.com\/svn\/trunk\/Rguide.xml\" title=\"Google's R Style Guide\" target=\"_blank\">Google&#8217;s R Style Guide<\/a>.<\/p>\n<p>Once we create a variable it&#8217;s added to the global environment, but we might also wish to remove a variable from the global environment, to do this execute<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nrm(&quot;x&quot;)\r\n\r\n# or use\r\n\r\nremove(&quot;x&quot;)\r\n<\/pre>\n<p>where x is the variable name we want to remove.<\/p>\n<p>Finally we can view the values within a variable using <\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nprint(x)\r\n\r\n# or simply typing the variable name and pressing enter\r\n\r\nx\r\n<\/pre>\n<p>either of the above will output the value stored within the variable x.<\/p>\n<p><strong>Scope\/Environments<\/strong><\/p>\n<p>If we simply assign variable (as previously discussed) the variable will be added to the environment\/global scope, however we can also create alternate scope known as a custom environment. <\/p>\n<p>We create a new environment using the followings<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\ne &lt;- new.env()\r\n<\/pre>\n<p>where e is a variable of type Environment. We can then assign variables to this environment using<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nassign(&quot;x&quot;, 123, e)\r\n\r\n# or \r\n\r\ne&#x5B;&#x5B;&quot;x&quot;]] &lt;- 123\r\n\r\n#or\r\n\r\ne$x &lt;- 123\r\n<\/pre>\n<p>in all of the above our new environment variable e is used and the variable x is created within the environment scope and assigned the value 123.<\/p>\n<p><em>Note: in the last example with the $ quotes are optional around the variable name<\/em><\/p>\n<p>To output the variable from the new Environment we can simply type<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\ne$x\r\n\r\n# or\r\n\r\nget(&quot;x&quot;, e)\r\n\r\n# or\r\n\r\ne&#x5B;&#x5B;&quot;x&quot;]]\r\n<\/pre>\n<p><strong>More on Environments<\/strong><\/p>\n<p>We can also get a variable from the global environment using get, such as <\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nget(&quot;x&quot;, globalenv()) \r\n<\/pre>\n<p>and if we want to find the parent environment to a custom environment we can use <\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nparent.env(e)\r\n<\/pre>\n<p><strong>Operators<\/strong><\/p>\n<p>We&#8217;ve already covered the assignment operator <-, let's look at some of the other available operators.\n\n<em>Arithmetic Operators<\/em><\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\na &lt;- 10  # assign a the value 10\r\nb &lt;- 5   # assign b the value 5\r\n\r\na + b    # addition operator\r\na - b    # subtraction operator\r\na * b    # multiplication operator\r\na \/ b    # division operator\r\na ^ b    # exponent operator\r\na ** b   # alternate exponent operator\r\na %% b   # modulus operator\r\na %\/% b  # integer division\r\n<\/pre>\n<p><em>Logical operators<\/em><\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\na &lt; b    # less than operator\r\na &gt; b    # greater than operator\r\na &lt;= b   # less than or equal operator\r\na &gt;= b   # less than or equal operator\r\na == b   # equality operator\r\na != b   # not equal operator\r\n!a       # logical NOT operator\r\na | y    # logical OR operator\r\na &amp; y    # logical AND operator\r\n<\/pre>\n<p>We can also use the function <em>isTRUE(a)<\/em> to test if a variable is TRUE.<\/p>\n<p>This covers the real basics of getting up and running with R. I hope to create further posts covering more aspects of R real soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This will be a simple &#8220;getting started&#8221; post on the R environment and language. R is a statistical language which is also heavily used in data mining and machine learning. For this post I&#8217;m using R version 3.1.2 and the free version of R Studio 0.98.1087. Assuming we have R installed and then R Studio, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[87],"tags":[],"class_list":["post-2489","post","type-post","status-publish","format-standard","hentry","category-r"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2489"}],"version-history":[{"count":24,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2489\/revisions"}],"predecessor-version":[{"id":2530,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2489\/revisions\/2530"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}