Skip to content

Commit

Permalink
Rerendered the book
Browse files Browse the repository at this point in the history
  • Loading branch information
dnieuw committed Jun 3, 2024
1 parent e06d87f commit 128352f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
4 changes: 2 additions & 2 deletions docs/3-2_functions_examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,8 @@ <h3 class="anchored" data-anchor-id="built-in-named-functions">Built-in Named Fu
<div class="cell">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">rnorm</span>(<span class="dv">10</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 0.80078468 0.71961071 -0.08707918 -0.57456933 0.21018752 -1.19981678
[7] -0.04798129 0.43013183 -0.38443686 -0.10556698</code></pre>
<pre><code> [1] -1.8104755 -0.3936216 0.4644255 -1.0156486 -1.2566601 2.3059345
[7] 1.0483187 -0.2918922 0.9020440 -1.6973320</code></pre>
</div>
</div>
<p>For a (very long) list of all named functions available in base-R have a look at this website:</p>
Expand Down
28 changes: 20 additions & 8 deletions docs/3-4_functions_custom.html
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,10 @@
<h2 id="toc-title">Table of contents</h2>

<ul>
<li><a href="#function-environments" id="toc-function-environments" class="nav-link active" data-scroll-target="#function-environments">Function environments</a>
<li><a href="#example-of-custom-function" id="toc-example-of-custom-function" class="nav-link active" data-scroll-target="#example-of-custom-function">Example of custom function</a></li>
<li><a href="#use-return-in-a-function" id="toc-use-return-in-a-function" class="nav-link" data-scroll-target="#use-return-in-a-function">Use return() in a function</a></li>
<li><a href="#build-a-custom-function" id="toc-build-a-custom-function" class="nav-link" data-scroll-target="#build-a-custom-function">Build a custom function</a></li>
<li><a href="#function-environments" id="toc-function-environments" class="nav-link" data-scroll-target="#function-environments">Function environments</a>
<ul class="collapse">
<li><a href="#global-and-local-variables" id="toc-global-and-local-variables" class="nav-link" data-scroll-target="#global-and-local-variables">Global and Local Variables</a></li>
</ul></li>
Expand Down Expand Up @@ -522,6 +525,8 @@ <h1 class="title">Creating your own functions</h1>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> <span class="do">## Here you type expressions that use the arguments</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<section id="example-of-custom-function" class="level3">
<h3 class="anchored" data-anchor-id="example-of-custom-function">Example of custom function</h3>
<p>Each line inside the function is an object assignment, a function call, a subsetting, a conditional statement, an if/else statement, a for loop, etc. - basically anything you have now learned how to do in R that you want the function to do!</p>
<p>Below is an easy example that calculates the mean of two values (<code>x</code> and <code>y</code>):</p>
<div class="cell">
Expand All @@ -543,6 +548,9 @@ <h1 class="title">Creating your own functions</h1>
<pre><code>[1] 4</code></pre>
</div>
</div>
</section>
<section id="use-return-in-a-function" class="level3">
<h3 class="anchored" data-anchor-id="use-return-in-a-function">Use return() in a function</h3>
<p>To have a function output something, you must return something. Either the value of the last command is returned (as in <code>mean_xy</code>) or you can use <code>return()</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>mean_xy <span class="ot">&lt;-</span> <span class="cf">function</span>(x, y){</span>
Expand Down Expand Up @@ -612,6 +620,9 @@ <h1 class="title">Creating your own functions</h1>
</div>
<p>Custom functions in R are useful if you have a bunch of commands that you have to use multiple times. By combining them in a function you 1) save time, 2) keep your code concise, and 3) make less coding mistakes.</p>
<p>In the next example a function called <code>my_descriptives</code> is made to calculate a mean of a vector only for the positive values.</p>
</section>
<section id="build-a-custom-function" class="level3">
<h3 class="anchored" data-anchor-id="build-a-custom-function">Build a custom function</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>my_descriptives <span class="ot">&lt;-</span> <span class="cf">function</span>(x){</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> x.trim <span class="ot">&lt;-</span> x[x<span class="sc">&gt;=</span><span class="dv">0</span>]</span>
Expand All @@ -625,22 +636,22 @@ <h1 class="title">Creating your own functions</h1>
<div class="cell">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>data<span class="sc">$</span>Ages</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 41 49 57 54 27 49 56 40 49 26 16 48 40 41 43 -50 58 62 56
[20] 36 42 47 30 57 44</code></pre>
<pre><code> [1] 46 58 47 22 45 51 46 28 53 55 53 55 59 30 53 -50 45 41 43
[20] 47 56 51 37 29 44</code></pre>
</div>
</div>
<p>There is one value <code>-50</code>, that is clearly an error.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">my_descriptives</span>(data<span class="sc">$</span>Ages)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 44.5</code></pre>
<pre><code>[1] 45.58333</code></pre>
</div>
</div>
<p>Compare the output with using the standard function <code>mean()</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mean</span>(data<span class="sc">$</span>Ages)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 40.72</code></pre>
<pre><code>[1] 41.76</code></pre>
</div>
</div>
<p>In the standard <code>mean</code> function, the negative outlier is included and influences the mean!</p>
Expand All @@ -656,15 +667,15 @@ <h1 class="title">Creating your own functions</h1>
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">my_descriptives</span>(data<span class="sc">$</span>Ages)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Min. 1st Qu. Median Mean 3rd Qu. Max.
16.0 40.0 45.5 44.5 54.5 62.0 </code></pre>
22.00 42.50 46.50 45.58 53.00 59.00 </code></pre>
</div>
</div>
<p>Again, let’s compare the output to the standard <code>summary()</code> function.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(data<span class="sc">$</span>Ages)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Min. 1st Qu. Median Mean 3rd Qu. Max.
-50.00 40.00 44.00 40.72 54.00 62.00 </code></pre>
-50.00 41.00 46.00 41.76 53.00 59.00 </code></pre>
</div>
</div>
<p>If you have multiple objects to return, you have to put them in an object container, like a list, vector, array or data.frame. It is not possible to return multiple individual objects like this:</p>
Expand All @@ -689,7 +700,7 @@ <h1 class="title">Creating your own functions</h1>
[1] 1

[[2]]
[1] 44.5</code></pre>
[1] 45.58333</code></pre>
</div>
</div>
<p>There was <code>1</code> value below zero, as provided by the first element in the list.</p>
Expand Down Expand Up @@ -724,6 +735,7 @@ <h1 class="title">Creating your own functions</h1>
<pre><code>[1] 4 3</code></pre>
</div>
</div>
</section>
<section id="function-environments" class="level2">
<h2 class="anchored" data-anchor-id="function-environments">Function environments</h2>
<p>Each function, whether built-in or user-defined, has an associated environment, which can be thought of as a container that holds all of the objects present at the time the function is created.</p>
Expand Down
8 changes: 4 additions & 4 deletions docs/3-5_functions_conditional.html
Original file line number Diff line number Diff line change
Expand Up @@ -613,27 +613,27 @@ <h3 class="anchored" data-anchor-id="if-else-statements-in-functions"><code>if()
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="fu">my_descriptives</span>(data<span class="sc">$</span>Ages)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Min. 1st Qu. Median Mean 3rd Qu. Max.
19.00 31.00 41.50 41.17 50.25 69.00 </code></pre>
30.00 35.00 45.00 44.67 49.75 66.00 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="fu">my_descriptives</span>(data<span class="sc">$</span>Sex)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>x.trim
0 1
16 8 </code></pre>
12 12 </code></pre>
</div>
</div>
<p>Again, we compare the output with the standard <code>summary()</code> and <code>table()</code> output:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(data<span class="sc">$</span>Ages)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Min. 1st Qu. Median Mean 3rd Qu. Max.
-50.00 31.00 39.00 37.52 50.00 69.00 </code></pre>
-50.00 35.00 44.00 40.88 49.00 66.00 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">table</span>(data<span class="sc">$</span>Sex)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
-1 0 1
1 16 8 </code></pre>
1 12 12 </code></pre>
</div>
</div>
<p>Apart form nesting <code>if()</code> statements it is also possible to string multiple if statements together like so:</p>
Expand Down
32 changes: 16 additions & 16 deletions docs/3-8_functions_apply.html
Original file line number Diff line number Diff line change
Expand Up @@ -650,23 +650,23 @@ <h3 class="anchored" data-anchor-id="using-tapply-on-groups-of-data">Using tappl
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>patients</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> group outcome
1 grp-1 -1.43681983
2 grp-1 -0.44166760
3 grp-1 -0.70621360
4 grp-1 1.25776159
5 grp-1 0.16538060
6 grp-1 1.02994392
7 grp-2 -0.51309936
8 grp-2 -2.66252109
9 grp-2 0.13120503
10 grp-2 0.69078366
11 grp-2 0.07324137
12 grp-2 -0.78011510</code></pre>
1 grp-1 0.08536396
2 grp-1 -2.31848310
3 grp-1 0.31685535
4 grp-1 0.28265549
5 grp-1 -1.26812212
6 grp-1 -0.40846661
7 grp-2 -1.06279780
8 grp-2 0.39937508
9 grp-2 -0.94402967
10 grp-2 1.05204023
11 grp-2 -0.13097807
12 grp-2 -0.79226230</code></pre>
</div>
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">tapply</span>(patients<span class="sc">$</span>outcome, patients<span class="sc">$</span>group, mean)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> grp-1 grp-2
-0.02193582 -0.51008425 </code></pre>
<pre><code> grp-1 grp-2
-0.5516995 -0.2464421 </code></pre>
</div>
</div>
<p>It is also possible to use multiple factors in a list to create groups, which returns a matrix.</p>
Expand All @@ -678,8 +678,8 @@ <h3 class="anchored" data-anchor-id="using-tapply-on-groups-of-data">Using tappl
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a><span class="fu">tapply</span>(patients<span class="sc">$</span>outcome, <span class="fu">list</span>(patients<span class="sc">$</span>group, patients<span class="sc">$</span>serotype), mean)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> A B
grp-1 -0.9617085 -0.3269053
grp-2 -1.1450631 -0.5657517</code></pre>
grp-1 0.2598798 0.6498257
grp-2 -0.5625088 -1.4830715</code></pre>
</div>
</div>
<p>These are some (trivial) examples of how you can use the apply family of functions to quickly apply a function to your data. It is possible to do the same thing by using for loops, but apply functions are generally faster to write and read. In some cases using apply to run your function can also increase the speed of your code. More on increasing the speed of your code will follow in later lectures.</p>
Expand Down
Loading

0 comments on commit 128352f

Please sign in to comment.