{"id":1757,"date":"2011-05-17T21:04:53","date_gmt":"2011-05-17T21:04:53","guid":{"rendered":"http:\/\/really.zonky.org\/?p=1757"},"modified":"2011-05-17T21:04:53","modified_gmt":"2011-05-17T21:04:53","slug":"my-r-cheat-sheet","status":"publish","type":"post","link":"https:\/\/really.zonky.org\/?p=1757","title":{"rendered":"My R Cheat Sheet"},"content":{"rendered":"<p>This is going to be grossly insufficient for anyone trying to learn <a href=\"http:\/\/www.r-project.org\/\">R<\/a> (a wonderfully powerful statistical package &#8230; although the &#8220;stats&#8221; part of that may be my inner statistician). This is merely a set of commands I tend to use myself on the rather too rare occasions when I need R.<\/p>\n<p>R is started with &#8220;R&#8221; at the command-line &#8230; of course (although &#8220;r&#8221; does something quite different).<\/p>\n<h2>Loading Data<\/h2>\n<p>If you have a file formatted like :-<\/p>\n<pre>number1 number2\r\nnumber3 number4<\/pre>\n<p>Then this can be read into a &#8220;variable&#8221; with :-<\/p>\n<pre>&gt; Data &lt;- read.table(\"filename.dat\")<\/pre>\n<p>If it is convenient to add column names into the file to give presentable names later on, then do so and tell R that there are headers :-<\/p>\n<pre>&gt; Data &lt;- read.table(\"filename.dat\", header=TRUE)<\/pre>\n<p>Whilst R has plenty of control about how to read data &#8230; far more than I need at least, it may be easiest to munge your input data into the above format if you are more comfortable with the command-line. You can see how R has imported your data with :-<\/p>\n<pre>&gt; names(Data)\r\n[1]: \"header1\" \"header2\"<\/pre>\n<p>Later when it comes to doing something with the actual data, you can access the relevant &#8220;vector&#8221; with <em>Data$header1<\/em>. But hopefully you will choose more meaningful names!<\/p>\n<h2>Stats Summary<\/h2>\n<p>To produce a summary of a vector :-<\/p>\n<pre>&gt; summary(vector)\r\n   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. \r\n   1249    6938   18900   16210   24100   30840<\/pre>\n<h2>Graphs<\/h2>\n<p>This section needs a <em>lot<\/em> of expansion. But to graph two variables &#8230; essentially one being a value at a particular time and the other being the time :-<\/p>\n<pre>plot(v1, v2, xlab=\"Horizontal label\", ylab=\"Vertical label\", main=\"Title\")<\/pre>\n<p>This will draw the graph onto the default device &#8211; which is normally the main X11 display. If you want to change the output, you need to choose an alternate device. For a PNG file :-<\/p>\n<pre>png(\"filename.png\")\r\n(Redo the plot you're happy with)\r\ndev.off()<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is going to be grossly insufficient for anyone trying to learn R (a wonderfully powerful statistical package &#8230; although the &#8220;stats&#8221; part of that may be my inner statistician). This is merely a set of commands I tend to use myself on the rather too rare occasions when I need R. R is started <a href='https:\/\/really.zonky.org\/?p=1757' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false,"_share_on_mastodon":"0"},"categories":[226],"tags":[],"class_list":["post-1757","post","type-post","status-publish","format-standard","hentry","category-working-notes","category-226-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1f2KI-sl","_links":{"self":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/1757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1757"}],"version-history":[{"count":3,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/1757\/revisions"}],"predecessor-version":[{"id":1760,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/1757\/revisions\/1760"}],"wp:attachment":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}