Skip to content

Commit

Permalink
Update in-course exercise chapter 1
Browse files Browse the repository at this point in the history
  • Loading branch information
geanders committed Aug 26, 2020
1 parent d43fd71 commit fb09f79
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 111 deletions.
10 changes: 5 additions & 5 deletions RProgrammingForResearch.log
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This is XeTeX, Version 3.14159265-2.6-0.999991 (TeX Live 2019) (preloaded format=xelatex 2019.5.8) 26 AUG 2020 01:37
This is XeTeX, Version 3.14159265-2.6-0.999991 (TeX Live 2019) (preloaded format=xelatex 2019.5.8) 26 AUG 2020 09:10
entering extended mode
restricted \write18 enabled.
%&-line parsing enabled.
Expand Down Expand Up @@ -4762,8 +4762,8 @@ File: RProgrammingForResearch_files/figure-latex/z-1.pdf Graphic file (type pdf
<use RProgrammingForResearch_files/figure-latex/z-1.pdf>
[401]
Overfull \hbox (1.5pt too wide) in paragraph at lines 18476--18476
[]\TU/lmtt/m/n/10 ## [1] 1.2641562 0.8838096 0.7132530 0.7915380 1.0801631 1.11
82116[]
[]\TU/lmtt/m/n/10 ## [1] 1.3152257 0.8662477 0.2124354 0.4976217 0.7574081 0.74
91137[]
[]

[402] [403] [404]
Expand Down Expand Up @@ -4884,7 +4884,7 @@ Overfull \hbox (437.25pt too wide) in paragraph at lines 19136--19136
[]

[418]
Overfull \hbox (342.6602pt too wide) in paragraph at lines 19151--19166
Overfull \hbox (351.05376pt too wide) in paragraph at lines 19151--19166
[][]
[]

Expand Down Expand Up @@ -5187,7 +5187,7 @@ LaTeX Warning: There were multiply-defined labels.
Here is how much of TeX's memory you used:
29030 strings out of 492916
560364 string characters out of 6133346
756422 words of memory out of 5000000
755422 words of memory out of 5000000
32575 multiletter control sequences out of 15000+600000
538876 words of font info for 95 fonts, out of 8000000 for 9000
1348 hyphenation exceptions out of 8191
Expand Down
Binary file modified docs/RProgrammingForResearch.pdf
Binary file not shown.
Binary file modified docs/RProgrammingForResearch_files/figure-html/gg-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/RProgrammingForResearch_files/figure-html/q-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/RProgrammingForResearch_files/figure-html/w-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/RProgrammingForResearch_files/figure-html/z-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 33 additions & 33 deletions docs/entering-and-cleaning-data-2.html
Original file line number Diff line number Diff line change
Expand Up @@ -686,19 +686,19 @@ <h2><span class="header-section-number">6.3</span> Longer data</h2>
<p>Here is some simulated wide data:</p>
<div class="sourceCode" id="cb531"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb531-1"><a href="entering-and-cleaning-data-2.html#cb531-1"></a>wide_stocks[<span class="dv">1</span><span class="op">:</span><span class="dv">3</span>, ]</span></code></pre></div>
<pre><code>## # A tibble: 3 x 4
## time X Y Z
## &lt;date&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
## 1 2009-01-01 0.902 -0.454 -7.09
## 2 2009-01-02 0.181 -1.87 -0.346
## 3 2009-01-03 -0.636 -3.00 -0.202</code></pre>
## time X Y Z
## &lt;date&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
## 1 2009-01-01 -0.141 2.00 6.45
## 2 2009-01-02 -0.857 -0.286 2.43
## 3 2009-01-03 -0.434 2.19 -6.25</code></pre>
<p>In the <code>wide_stocks</code> dataset, there are separate columns for three different stocks (<code>X</code>, <code>Y</code>, and <code>Z</code>). Each cell gives the value for a certain stock on a certain day. This data isn’t “tidy”, because the identify of the stock (<code>X</code>, <code>Y</code>, or <code>Z</code>) is a variable, and you’ll probably want to include it as a variable in modeling.</p>
<div class="sourceCode" id="cb533"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb533-1"><a href="entering-and-cleaning-data-2.html#cb533-1"></a>wide_stocks[<span class="dv">1</span><span class="op">:</span><span class="dv">3</span>, ]</span></code></pre></div>
<pre><code>## # A tibble: 3 x 4
## time X Y Z
## &lt;date&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
## 1 2009-01-01 0.902 -0.454 -7.09
## 2 2009-01-02 0.181 -1.87 -0.346
## 3 2009-01-03 -0.636 -3.00 -0.202</code></pre>
## time X Y Z
## &lt;date&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
## 1 2009-01-01 -0.141 2.00 6.45
## 2 2009-01-02 -0.857 -0.286 2.43
## 3 2009-01-03 -0.434 2.19 -6.25</code></pre>
<p>If you want to convert the dataframe to have all stock values in a single column, you can use <code>pivot_longer</code> to convert wide data to long data:</p>
<div class="sourceCode" id="cb535"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb535-1"><a href="entering-and-cleaning-data-2.html#cb535-1"></a>long_stocks &lt;-<span class="st"> </span><span class="kw">pivot_longer</span>(<span class="dt">data =</span> wide_stocks,</span>
<span id="cb535-2"><a href="entering-and-cleaning-data-2.html#cb535-2"></a> <span class="dt">cols =</span> <span class="op">-</span>time,</span>
Expand All @@ -708,21 +708,21 @@ <h2><span class="header-section-number">6.3</span> Longer data</h2>
<pre><code>## # A tibble: 5 x 3
## time stock price
## &lt;date&gt; &lt;chr&gt; &lt;dbl&gt;
## 1 2009-01-01 X 0.902
## 2 2009-01-01 Y -0.454
## 3 2009-01-01 Z -7.09
## 4 2009-01-02 X 0.181
## 5 2009-01-02 Y -1.87</code></pre>
## 1 2009-01-01 X -0.141
## 2 2009-01-01 Y 2.00
## 3 2009-01-01 Z 6.45
## 4 2009-01-02 X -0.857
## 5 2009-01-02 Y -0.286</code></pre>
<p>In this “longer” dataframe, there is now one column that gives the identify of the stock (<code>stock</code>) and another column that gives the price of that stock that day (<code>price</code>):</p>
<div class="sourceCode" id="cb537"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb537-1"><a href="entering-and-cleaning-data-2.html#cb537-1"></a>long_stocks[<span class="dv">1</span><span class="op">:</span><span class="dv">5</span>, ]</span></code></pre></div>
<pre><code>## # A tibble: 5 x 3
## time stock price
## &lt;date&gt; &lt;chr&gt; &lt;dbl&gt;
## 1 2009-01-01 X 0.902
## 2 2009-01-01 Y -0.454
## 3 2009-01-01 Z -7.09
## 4 2009-01-02 X 0.181
## 5 2009-01-02 Y -1.87</code></pre>
## 1 2009-01-01 X -0.141
## 2 2009-01-01 Y 2.00
## 3 2009-01-01 Z 6.45
## 4 2009-01-02 X -0.857
## 5 2009-01-02 Y -0.286</code></pre>
<p>The format for a <code>pivots_longer</code> call is:</p>
<div class="sourceCode" id="cb539"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb539-1"><a href="entering-and-cleaning-data-2.html#cb539-1"></a><span class="co">## Generic code</span></span>
<span id="cb539-2"><a href="entering-and-cleaning-data-2.html#cb539-2"></a>new_df &lt;-<span class="st"> </span><span class="kw">pivot_longer</span>(old_df,</span>
Expand All @@ -747,13 +747,13 @@ <h2><span class="header-section-number">6.3</span> Longer data</h2>
<span id="cb541-3"><a href="entering-and-cleaning-data-2.html#cb541-3"></a> <span class="dt">values_from =</span> price)</span>
<span id="cb541-4"><a href="entering-and-cleaning-data-2.html#cb541-4"></a>stocks[<span class="dv">1</span><span class="op">:</span><span class="dv">5</span>, ]</span></code></pre></div>
<pre><code>## # A tibble: 5 x 4
## time X Y Z
## &lt;date&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
## 1 2009-01-01 0.902 -0.454 -7.09
## 2 2009-01-02 0.181 -1.87 -0.346
## 3 2009-01-03 -0.636 -3.00 -0.202
## 4 2009-01-04 1.28 0.902 -3.60
## 5 2009-01-05 -3.02 -0.914 5.74</code></pre>
## time X Y Z
## &lt;date&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
## 1 2009-01-01 -0.141 2.00 6.45
## 2 2009-01-02 -0.857 -0.286 2.43
## 3 2009-01-03 -0.434 2.19 -6.25
## 4 2009-01-04 -1.78 1.29 -2.42
## 5 2009-01-05 -0.0842 3.67 -2.05</code></pre>
<p>Notice that this reverses the action of <code>pivot_longer</code>.</p>
<p>The “wider” your data the less likely it is to be tidy, so won’t use <code>pivot_wider</code> frequently when you are preparing data for analysis. However, <code>pivot_wider</code> can be very helpful in creating tables for final reports and presentations.</p>
<p>For example, if you wanted to create a table with means and standard deviations for each of the three stocks, you could use <code>pivot_wider</code> to rearrange the final summary to create an attractive table.</p>
Expand All @@ -765,9 +765,9 @@ <h2><span class="header-section-number">6.3</span> Longer data</h2>
<pre><code>## # A tibble: 3 x 4
## stock N mean sd
## &lt;chr&gt; &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt;
## 1 X 10 0.182 1.30
## 2 Y 10 -0.289 2.52
## 3 Z 10 -2.30 3.59</code></pre>
## 1 X 10 -0.333 0.746
## 2 Y 10 1.12 2.69
## 3 Z 10 -1.11 3.63</code></pre>
<div class="sourceCode" id="cb547"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb547-1"><a href="entering-and-cleaning-data-2.html#cb547-1"></a>stock_summary <span class="op">%&gt;%</span></span>
<span id="cb547-2"><a href="entering-and-cleaning-data-2.html#cb547-2"></a><span class="st"> </span><span class="kw">mutate</span>(<span class="st">&quot;Mean (Std.dev.)&quot;</span> =<span class="st"> </span><span class="kw">paste0</span>(<span class="kw">round</span>(mean, <span class="dv">2</span>), <span class="st">&quot; (&quot;</span>,</span>
<span id="cb547-3"><a href="entering-and-cleaning-data-2.html#cb547-3"></a> <span class="kw">round</span>(sd, <span class="dv">2</span>), <span class="st">&quot;)&quot;</span>)) <span class="op">%&gt;%</span></span>
Expand All @@ -794,9 +794,9 @@ <h2><span class="header-section-number">6.3</span> Longer data</h2>
</tr>
<tr class="even">
<td align="left">Mean (Std.dev.)</td>
<td align="left">0.18 (1.3)</td>
<td align="left">-0.29 (2.52)</td>
<td align="left">-2.3 (3.59)</td>
<td align="left">-0.33 (0.75)</td>
<td align="left">1.12 (2.69)</td>
<td align="left">-1.11 (3.63)</td>
</tr>
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion docs/exploring-data-2.html
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ <h2><span class="header-section-number">7.3</span> Lists</h2>
<pre><code>## [1] &quot;coefficients&quot; &quot;residuals&quot; &quot;fitted.values&quot;</code></pre>
<div class="sourceCode" id="cb710"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb710-1"><a href="exploring-data-2.html#cb710-1"></a>my_mod[[<span class="st">&quot;coefficients&quot;</span>]]</span></code></pre></div>
<pre><code>## (Intercept) c(1:10)
## 0.2759347 -0.1316436</code></pre>
## -1.030100 0.163329</code></pre>
<p>A list can even contain other lists. We can use the <code>str</code> function to see the structure of a list:</p>
<div class="sourceCode" id="cb712"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb712-1"><a href="exploring-data-2.html#cb712-1"></a>a_list &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="kw">list</span>(<span class="st">&quot;a&quot;</span>, <span class="st">&quot;b&quot;</span>), <span class="kw">list</span>(<span class="dv">1</span>, <span class="dv">2</span>))</span>
<span id="cb712-2"><a href="exploring-data-2.html#cb712-2"></a></span>
Expand Down
Loading

0 comments on commit fb09f79

Please sign in to comment.