]> code.communitydata.science - stats_class_2020.git/commitdiff
updated built versions of w3 tutorial
authoraaronshaw <aaron.d.shaw@gmail.com>
Tue, 22 Sep 2020 19:57:20 +0000 (14:57 -0500)
committeraaronshaw <aaron.d.shaw@gmail.com>
Tue, 22 Sep 2020 19:57:20 +0000 (14:57 -0500)
r_tutorials/w03-R_tutorial.html
r_tutorials/w03-R_tutorial.pdf

index a11adec1353742c3908232a2189dd21e9d1ad319..8618cb5fa7b30aa760b1462b744eeda1f5b3611b 100644 (file)
@@ -434,7 +434,7 @@ MTS 525</h3>
 </div>
 <div id="adding-comments-to-your-r-code-chunks" class="section level3">
 <h3>Adding comments to your R code chunks</h3>
-<p>The concept of “comments” in code is pretty intuitive. A comment is just some text that the programming language interpreter ignores and repeats. Comments are generally inserted in code to make it easier for people to read in various ways. R interprets the <code>#</code> character and anything that comes after it as a comment. R will not try to interpret whatever comes next as a command:</p>
+<p>The concept of “comments” in code is potentially intuitive. A comment is just some text that the programming language interpreter ignores and repeats. Comments are generally inserted in code to make it easier for people to read in various ways. R interprets the <code>#</code> character and anything that comes after it as a comment. R will not try to interpret whatever comes next as a command:</p>
 <pre class="r"><code>2+2 </code></pre>
 <pre><code>## [1] 4</code></pre>
 <pre class="r"><code># This is a comment. The next line is too:
@@ -443,7 +443,58 @@ MTS 525</h3>
 </div>
 <div id="importing-datasets-from-libraries-the-web-and-locally-on-your-computer" class="section level3">
 <h3>Importing datasets from libraries, the web, and locally (on your computer)</h3>
-<p><TODOTODOTODO>. You will want to import datasets.</p>
+<p>Data import is crucial and can be a time-consuming step in quantitative/computational research (maybe especially in R). In the previous tutorial and problem set you needed to load a library/package in R. Many packages come with datasets pre-installed that you will use for assignments in the course and/or to try out example code. You will also need to learn how to import datasets from the web and locally from files stored on your computer. Here are examples of each.</p>
+<div id="loading-a-dataset-from-an-r-package" class="section level4">
+<h4>Loading a dataset from an R package</h4>
+<p>Let’s find the <code>email50</code> dataset that’s included in the <code>openintro</code> package provided by the textbook authors. First, I’ll load the library, then I can use the <code>data()</code> command to call the dataset.</p>
+<pre class="r"><code>library(openintro)</code></pre>
+<pre><code>## Loading required package: airports</code></pre>
+<pre><code>## Loading required package: cherryblossom</code></pre>
+<pre><code>## Loading required package: usdata</code></pre>
+<pre class="r"><code>data(email50)
+
+## Take a look at the first few rows of the email50 dataset
+head(email50)</code></pre>
+<pre><code>## # A tibble: 6 x 21
+##    spam to_multiple  from    cc sent_email time                image attach
+##   &lt;dbl&gt;       &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt;      &lt;dbl&gt; &lt;dttm&gt;              &lt;dbl&gt;  &lt;dbl&gt;
+## 1     0           0     1     0          1 2012-01-04 07:19:16     0      0
+## 2     0           0     1     0          0 2012-02-16 14:10:06     0      0
+## 3     1           0     1     4          0 2012-01-04 09:36:23     0      2
+## 4     0           0     1     0          0 2012-01-04 11:49:52     0      0
+## 5     0           0     1     0          0 2012-01-27 03:34:45     0      0
+## 6     0           0     1     0          0 2012-01-17 11:31:57     0      0
+## # … with 13 more variables: dollar &lt;dbl&gt;, winner &lt;fct&gt;, inherit &lt;dbl&gt;,
+## #   viagra &lt;dbl&gt;, password &lt;dbl&gt;, num_char &lt;dbl&gt;, line_breaks &lt;int&gt;,
+## #   format &lt;dbl&gt;, re_subj &lt;dbl&gt;, exclaim_subj &lt;dbl&gt;, urgent_subj &lt;dbl&gt;,
+## #   exclaim_mess &lt;dbl&gt;, number &lt;fct&gt;</code></pre>
+</div>
+<div id="loading-a-dataset-from-the-web" class="section level4">
+<h4>Loading a dataset from the web</h4>
+<p>This gets a bit more complicated because you have to use the <code>url()</code> command to tell R the address you want to use, then you will need to use a second command to actually import the dataset file. In this case, I’m going to point to another dataset provided by the OpenIntro authors containing NOAA temperature information (<a href="https://www.openintro.org/data/index.php?data=climate70">more information about the dataset is available on the OpenIntro website</a>). The format for the file is <code>.rda</code> which is one of several common R dataset file format suffixes (another one is .rdata) and R you’ll usually use the <code>load()</code> command to import an .rda or .rdata file.</p>
+<pre class="r"><code>load(url(&quot;https://www.openintro.org/data/rda/climate70.rda&quot;))
+
+## Again, check out the first few rows to see what you&#39;ve got.
+head(climate70)</code></pre>
+<pre><code>##       station latitude  longitude dx70_1948 dx70_2018 dx90_1948 dx90_2018
+## 1 USC00203823 41.93520  -84.64110       131       147        11        16
+## 2 USC00276818 44.25800  -71.25250        80        99         1         1
+## 3 USC00186620 39.41317  -79.40025       143       150         4         1
+## 4 USC00331890 40.24030  -81.87100       156       158        18        15
+## 5 USC00235987 37.83950  -94.37400       216       175        59        51
+## 6 USC00395691 45.56550 -100.44880       138       132        39        18</code></pre>
+</div>
+<div id="loading-a-dataset-stored-locally" class="section level4">
+<h4>Loading a dataset stored locally</h4>
+<p>Loading from local storage is last because, ironically, it may be the least intuitive. The best practice here is to use an <a href="https://en.wikipedia.org/wiki/Path_%28computing%29">absolute path</a> to point R to the unique location on your computer where the file in question is stored. In the example below, my code reflects the operating system and directory structure of my laptop. Your computer will likely (I assume/hope!) use something quite different. Nevertheless, I am providing an example because I think you may be able to work with it and it can at least provide a demonstration that we can talk about later on.</p>
+<pre class="r"><code>load(&quot;/home/ads/Documents/Teaching/2020/stats/data/week_03/group_07.RData&quot;)
+
+ls() ## list objects in my global environment</code></pre>
+<pre><code>## [1] &quot;climate70&quot; &quot;d&quot;         &quot;email50&quot;</code></pre>
+<pre class="r"><code>head(d) ## and inspect the first few rows of the new object</code></pre>
+<pre><code>## [1] -2452.018457     2.637751     3.241824     1.183585 15746.070789
+## [6]    65.013141</code></pre>
+</div>
 </div>
 </div>
 <div id="more-complicated-variable-types" class="section level2">
index aa65a2ab6252610524c1e08a0b02966944da497b..b764930b3a43c2bfad15e5d68ae82368014643b7 100644 (file)
Binary files a/r_tutorials/w03-R_tutorial.pdf and b/r_tutorials/w03-R_tutorial.pdf differ

Community Data Science Collective || Want to submit a patch?