{"id":13,"date":"2016-01-31T23:36:23","date_gmt":"2016-01-31T23:36:23","guid":{"rendered":"https:\/\/paulgcurran.com\/freeideas\/?p=13"},"modified":"2016-01-31T23:36:23","modified_gmt":"2016-01-31T23:36:23","slug":"learning-r-part-iiia-basic-commands","status":"publish","type":"post","link":"https:\/\/paulgcurran.com\/freeideas\/2016\/01\/31\/learning-r-part-iiia-basic-commands\/","title":{"rendered":"Learning R, part IIIa: Basic commands"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26 size-full\" src=\"https:\/\/paulgcurran.com\/freeideas\/wp-content\/uploads\/2016\/01\/Rplot02a.png\" alt=\"Rplot02a\" width=\"1000\" height=\"764\" srcset=\"https:\/\/paulgcurran.com\/freeideas\/wp-content\/uploads\/2016\/01\/Rplot02a.png 1000w, https:\/\/paulgcurran.com\/freeideas\/wp-content\/uploads\/2016\/01\/Rplot02a-300x229.png 300w, https:\/\/paulgcurran.com\/freeideas\/wp-content\/uploads\/2016\/01\/Rplot02a-768x587.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>One of the things that makes the initial learning curve of R so steep is that R doesn&#8217;t have a point and click, menu based, graphical user interface. Learning R is to some degree synonymous with learning the R programming language.<\/p>\n<p>Don&#8217;t let the words &#8216;programming language&#8217; stop you here.\u00a0While it might seem daunting at first &#8211; especially if this is your first contact with a programming language &#8211; it&#8217;s honestly not that bad.<\/p>\n<p>We&#8217;ve already explored some of the way the R command line works in the last two posts. You&#8217;ve seen that R can be used like a calculator to reduce mathematical expressions.<\/p>\n<p>In many ways, the rules of even basic mathematics can be viewed as a programming language by which you communicate commands to a program like R. The expression of &#8216;3+4&#8217; takes on a very specific form in order to convey the intention of adding the number 3 to the number 4. Entering the form &#8216;+34&#8242; or &#8217;34+&#8217; gives incorrect responses if the goal is to add 3 to 4.<\/p>\n<p>While the expression &#8216;4+3&#8217; is equivalent here, the same isn&#8217;t true for all the basic operators (&#8216;3\/4&#8217; is not equivalent to &#8216;4\/3&#8217;). The way we form these expressions adheres to a ruleset that you probably haven&#8217;t had to think of since grade school. The reason is exposure and practice. Use a programming language long enough and it will start to feel as natural as addition and subtraction.<\/p>\n<p>In addition to calculations, we&#8217;ve already seen a few other basic commands in the first few posts. Among these, and one of the commands you will use the most in R, is &#8216;&lt;-&#8216;<\/p>\n<p>Recall that the command:<\/p>\n<p>x&lt;-3<\/p>\n<p>Stores a value of 3 into the variable x. The reason that you&#8217;ll use this frequently is that it is exceptionally powerful. Being able to store values to variables is foundational to many of the more complex things you&#8217;ll do in R.<\/p>\n<p>Now, it&#8217;s worth noting that this is not the only way to store values to a variable. The following are both equivalent to the above:<\/p>\n<p>3-&gt;x<\/p>\n<p>x=3<\/p>\n<p>While using the forward arrow (&#8216;-&gt;&#8217;) might not be particularly enticing, it&#8217;s easy to get into the mindset that &#8216;=&#8217; would be an easier way to handle variable assignment. While there&#8217;s nothing stopping you, the general convention is to use &#8216;&lt;-&#8216;.<\/p>\n<p>This isn&#8217;t entirely arbitrary. While I can&#8217;t say for sure why this convention was established, there are a number of reasons why it makes sense. The most convincing of these is that the equal sign will be used for a number of other things, and there&#8217;s simply no reason to use it here. By saving it for tests of equivalence (&#8216;==&#8217;) and definition of parameters within functions we can make things easier down the line.<\/p>\n<p>Since we&#8217;re talking about it, you can use &#8216;==&#8217; as a test of equivalence in R, and R will return a value of TRUE or FALSE depending on if the two elements being equated are equal or not. Try it with:<\/p>\n<p>3==3<\/p>\n<p>3==4<\/p>\n<p>1+1==2<\/p>\n<p>1+1==4<\/p>\n<p>This will be particularly useful later, but is really just as simple as that at the moment. Store that one away, and just use &#8216;&lt;-&#8216; instead of &#8216;=&#8217; to assign values to variables.<\/p>\n<p>Moving on, we&#8217;ve seen that we can use the function c() to group a number of values as one object. The most frequent use of c() is with variable assignment above, to create one variable that contains a number of elements. This isn&#8217;t always necessary, though, depending on what you&#8217;re looking to do.<\/p>\n<p>If you simply type c(1,2,3,4) into the command line, R will, not surprisingly, return the values:<\/p>\n<p>1 2 3 4<\/p>\n<p>We could store those values into a variable with the command:<\/p>\n<p>x&lt;-c(1,2,3,4)<\/p>\n<p>But, even without variable assignment, we could use c() as part of a mathematical expression. Suppose you wanted to convert a group of temperatures from Celsius to Fahrenheit. The formula for conversion is:<\/p>\n<p>C*1.8+32=F<\/p>\n<p>If we just type the first part of this expression into R, it will give us the temperature value in Fahrenheit. You can try this by typing the expression:<\/p>\n<p>10*1.8+32<\/p>\n<p>R should return a value of 50 (degrees).<\/p>\n<p>If we wanted to check a number of temperatures all at once, we could take advantage of c() instead of running the same equation multiple times.<\/p>\n<p>If you type the expression:<\/p>\n<p>c(0,5,10,15,20,25)*1.8+32<\/p>\n<p>R should return the following values:<\/p>\n<p>32 41 50 59 68 77<\/p>\n<p>Again, it might make more sense to store your Celsius values into a variable, which is how you&#8217;d normally see this done. That is:<\/p>\n<p>x&lt;-c(0,5,10,15,20,25)<\/p>\n<p>x*1.8+32<\/p>\n<p>This should give the same result, and if you wanted you could even store that set of results as a variable by altering the second line to:<\/p>\n<p>y&lt;-x*1.8+32<\/p>\n<p>Now, say we wanted to do the same thing, but instead of going from 0 to 25 we wanted to go from 0 to 100, in steps of 5. We could sit down and write out all the values, but there&#8217;s an easier way by using the function seq().<\/p>\n<p>I&#8217;ve called c() a function above, without really going into what that means. A function is basically a command that takes some number of inputs and returns a result. The function c() takes all the values inside it and combines them into one set of values. The function seq() is only slightly more complicated.<\/p>\n<p>If you simply type:<\/p>\n<p>seq(10)<\/p>\n<p>R will output the values:<\/p>\n<p>1 2 3 4 5 6 7 8 9 10<\/p>\n<p>If we put in 100, R will give us the values from 1 to 100. There is a lot we can start to learn about R from this.<\/p>\n<p>One of the main things we can learn is that if we forget what a function in R does, we can use the function help() to find out. In this case, by typing:<\/p>\n<p>help(&#8220;seq&#8221;)<\/p>\n<p>This pulls up a help file on this function, and defines the parameters that the function accepts. There a number of parameters, and they all have defaults. We can interpret this from the following in the help file:<\/p>\n<p class=\"p1\"><span class=\"s1\">seq(from = 1, to = 1, by = ((to &#8211; from)\/(length.out &#8211; 1)),\u00a0<\/span><span class=\"s1\">length.out = NULL, along.with = NULL, &#8230;)<\/span><\/p>\n<p class=\"p1\">Forget about the later parts for now, but take note that the general form is:<\/p>\n<p class=\"p1\">seq(from,to,by)<\/p>\n<p class=\"p1\">If we only give one value, R interprets it to be the &#8216;to&#8217; value, as that&#8217;s really all that can be done with one value (on the assumption that from and by are both 1 by default). We can add other values, and if we want to be organized we can even explicitly define them:<\/p>\n<p class=\"p1\">seq(from=1,to=10)<\/p>\n<p class=\"p1\">This should give the same output as seq(10) above, and will also give the same output as seq(1,10). It&#8217;s a good habit to explicitly define parameters while you&#8217;re first learning R, and a great habit to do it later. Fortunately or unfortunately, it&#8217;s not mandatory.<\/p>\n<p class=\"p1\">Keep in mind that what we&#8217;re looking to do is produce a set of numbers from 0 to 100 by steps of 5. That language is pretty close to the language that seq() can understand, so all we have to do is tweak it into the correct form:<\/p>\n<p class=\"p1\">seq(from=0,to=100,by=5)<\/p>\n<p class=\"p1\">This should give you the output:<\/p>\n<p class=\"p1\">0 \u00a0 5 \u00a010 \u00a015 \u00a020 \u00a025 \u00a030 \u00a035 \u00a040 \u00a045 \u00a050 \u00a055 \u00a060 \u00a065 \u00a070 \u00a075 \u00a080 \u00a085 \u00a090 \u00a095 100<\/p>\n<p>Again, you can explicitly set those parameters by identifying each parameter that you&#8217;re setting, or just take advantage of the fact that the seq() function expects them in a certain order. That is, the following line is equivalent to the above:<\/p>\n<p>seq(0,100,5)<\/p>\n<p>We can now incorporate this into our earlier expression without having to use a c() function:<\/p>\n<p>seq(0,100,5)*1.8+32<\/p>\n<p>If you&#8217;ve been paying attention to the temperatures, the endpoints of the output shouldn&#8217;t be too shocking:<\/p>\n<p>32 \u00a041 \u00a050 \u00a059 \u00a068 \u00a077 \u00a086 \u00a095 104 113 122 131 140 149 158 167 176 185 194 203 212<\/p>\n<p>You might notice that each of these numbers is separated by 9 degrees Fahrenheit. That&#8217;s no coincidence &#8211; I&#8217;ve been using the conversion of:<\/p>\n<p>C*1.8+32<\/p>\n<p>1.8 can also be expressed as 9\/5, so another way to write it is:<\/p>\n<p>C*(9\/5)+32<\/p>\n<p>That is, every time the temperature goes up 5 degrees on the Celsius scale, it equates to a 9 degree temperature increase on the Fahrenheit scale. That&#8217;s what we&#8217;re seeing in our data above. Once we&#8217;re in Fahrenheit scaled units we have to add 32 degrees to account for the shift between the zero anchor point of the scales. It&#8217;s as simple as that.<\/p>\n<p>Obviously, seq() can be used for a lot of different things. Play around with it, and see what you can create. Or, try to figure out the code below:<\/p>\n<p>C&lt;-seq(0,100,5)<br \/>\nF&lt;-C*1.8+32<br \/>\ncolFunction&lt;-colorRampPalette(c(&#8220;blue&#8221;,&#8221;red&#8221;))<br \/>\nplot(C,F,col=colFunction(21))<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the things that makes the initial learning curve of R so steep is that R doesn&#8217;t have a point and click, menu based, graphical user interface. Learning R is to some degree synonymous with learning the R programming language. Don&#8217;t let the words &#8216;programming language&#8217; stop you here.\u00a0While it might seem daunting at &hellip; <a href=\"https:\/\/paulgcurran.com\/freeideas\/2016\/01\/31\/learning-r-part-iiia-basic-commands\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learning R, part IIIa: Basic commands&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/paulgcurran.com\/freeideas\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paulgcurran.com\/freeideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paulgcurran.com\/freeideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paulgcurran.com\/freeideas\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paulgcurran.com\/freeideas\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":4,"href":"https:\/\/paulgcurran.com\/freeideas\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":27,"href":"https:\/\/paulgcurran.com\/freeideas\/wp-json\/wp\/v2\/posts\/13\/revisions\/27"}],"wp:attachment":[{"href":"https:\/\/paulgcurran.com\/freeideas\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paulgcurran.com\/freeideas\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paulgcurran.com\/freeideas\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}