Skip to content

Commit

Permalink
Render site
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Oct 10, 2024
1 parent c9db86b commit ffc09e7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
8 changes: 4 additions & 4 deletions help.html
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,14 @@ <h2><strong>Why are my changes not taking effect? It’s making my results look
<p>Here we are creating a new object from an existing one:</p>
<pre class="r"><code>new_rivers &lt;- sample(rivers, 5)
new_rivers</code></pre>
<pre><code>## [1] 320 1770 680 259 780</code></pre>
<pre><code>## [1] 618 720 625 425 1270</code></pre>
<p>Using just this will only print the result and not actually change <code>new_rivers</code>:</p>
<pre class="r"><code>new_rivers + 1</code></pre>
<pre><code>## [1] 321 1771 681 260 781</code></pre>
<pre><code>## [1] 619 721 626 426 1271</code></pre>
<p>If we want to modify <code>new_rivers</code> and save that modified version, then we need to reassign <code>new_rivers</code> like so:</p>
<pre class="r"><code>new_rivers &lt;- new_rivers + 1
new_rivers</code></pre>
<pre><code>## [1] 321 1771 681 260 781</code></pre>
<pre><code>## [1] 619 721 626 426 1271</code></pre>
<p>If we forget to reassign this can cause subsequent steps to not work as expected because we will not be working with the data that has been modified.</p>
<hr />
</div>
Expand Down Expand Up @@ -409,7 +409,7 @@ <h2><strong>Error: object ‘X’ not found</strong></h2>
<p>Make sure you run something like this, with the <code>&lt;-</code> operator:</p>
<pre class="r"><code>rivers2 &lt;- new_rivers + 1
rivers2</code></pre>
<pre><code>## [1] 322 1772 682 261 782</code></pre>
<pre><code>## [1] 620 722 627 427 1272</code></pre>
<hr />
</div>
<div id="error-unexpected-in-error-unexpected-in-error-unexpected-x-in" class="section level2">
Expand Down
60 changes: 42 additions & 18 deletions modules/Functions/lab/Functions_Lab.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,31 +167,43 @@ <h1 class="title toc-ignore">Functions Lab</h1>

<div id="part-1" class="section level1">
<h1>Part 1</h1>
<p>Load all the libraries we will use in this lab.</p>
<p>Load the <code>tidyverse</code> package.</p>
<pre class="r"><code>library(tidyverse)</code></pre>
<div id="section" class="section level3">
<h3>1.1</h3>
<p>Create a function that takes one argument, a vector, and returns the sum of the vector and then squares the result. Call it “sum_squared”. Test your function on the vector <code>c(2,7,21,30,90)</code> - you should get the answer 22500.</p>
<pre><code># General format
NEW_FUNCTION &lt;- function(x, y) x + y</code></pre>
<p>or</p>
<pre><code># General format
NEW_FUNCTION &lt;- function(x, y){
result &lt;- x + y
return(result)
}</code></pre>
<p>Create a function that:</p>
<ul>
<li>Takes one argument, a vector.</li>
<li>Returns the sum of the vector and then squares the result.</li>
<li>Call it “sum_squared”.</li>
<li>Test your function on the vector <code>c(2,7,21,30,90)</code> - you should get the answer 22500.</li>
<li>Format is <code>NEW_FUNCTION &lt;- function(x, y) x + y</code></li>
</ul>
</div>
<div id="section-1" class="section level3">
<h3>1.2</h3>
<p>Create a function that takes two arguments, (1) a vector and (2) a numeric value. This function tests whether the number (2) is contained within the vector (1). <strong>Hint</strong>: use <code>%in%</code>. Call it <code>has_n</code>. Test your function on the vector <code>c(2,7,21,30,90)</code> and number <code>21</code> - you should get the answer TRUE.</p>
<p>Create a function that:</p>
<ul>
<li>takes two arguments, (1) a vector and (2) a numeric value.</li>
<li>This function tests whether the number (2) is contained within the vector (1). <strong>Hint</strong>: use <code>%in%</code>.</li>
<li>Call it <code>has_n</code>.</li>
<li>Test your function on the vector <code>c(2,7,21,30,90)</code> and number <code>21</code> - you should get the answer TRUE.</li>
</ul>
</div>
<div id="section-2" class="section level3">
<h3>1.3</h3>
<p>Amend the function <code>has_n</code> from question 1.2 so that it takes a default value of <code>21</code> for the numeric argument.</p>
</div>
<div id="p.1" class="section level3">
<h3>P.1</h3>
<p>Create a new number <code>b_num</code> that is not contained with <code>nums</code>. Use your updated <code>has_n</code> function with the default value and add <code>b_num</code> as the <code>n</code> argument when calling the function. What is the outcome?</p>
<p>Create a function for the CalEnviroScreen Data.</p>
<ul>
<li>Read in (<a href="https://daseh.org/data/CalEnviroScreen_data.csv" class="uri">https://daseh.org/data/CalEnviroScreen_data.csv</a>)</li>
<li>The function takes an argument for a column name. (use <code>{{col_name}}</code>)</li>
<li>The function creates a ggplot with <code>{{col_name}}</code> on the x-axis and <code>Poverty</code> on the y-axis.</li>
<li>Use <code>geom_point()</code></li>
<li>Test the function using the <code>Lead</code> column and <code>HousingBurden</code> columns, or other columns of your choice.</li>
</ul>
</div>
</div>
<div id="part-2" class="section level1">
Expand All @@ -202,7 +214,13 @@ <h3>2.1</h3>
</div>
<div id="section-4" class="section level3">
<h3>2.2</h3>
<p>We want to get some summary statistics on water contamination. Use <code>across</code> inside <code>summarize</code> to get the sum total variable containing the string “water” AND ending with “Pctl”. <strong>Hint</strong>: use <code>contains()</code> AND <code>ends_with()</code> to select the right columns inside <code>across</code>. Remember that <code>NA</code> values can influence calculations.</p>
<p>We want to get some summary statistics on water contamination.</p>
<ul>
<li>Use <code>across</code> inside <code>summarize</code>.</li>
<li>Choose columns about “water”. <strong>Hint</strong>: use <code>contains(&quot;water&quot;)</code> inside <code>across</code>.</li>
<li>Use <code>mean</code> as the function inside of <code>across</code>.</li>
<li>Remember that <code>NA</code> values can influence calculations.</li>
</ul>
<pre><code># General format
data %&gt;%
summarize(across(
Expand All @@ -212,7 +230,13 @@ <h3>2.2</h3>
</div>
<div id="section-5" class="section level3">
<h3>2.3</h3>
<p>Use <code>across</code> and <code>mutate</code> to convert all columns containing the word “water” into proportions (i.e., divide that value by 100). <strong>Hint</strong>: use <code>contains()</code> to select the right columns within <code>across()</code>. Use an anonymous function (“function on the fly”) to divide by 100 (<code>function(x) x / 100</code>). It will also be easier to check your work if you <code>select()</code> columns that match “Pctl”.</p>
<p>Convert all columns that are percentiles into proportions.</p>
<ul>
<li>Use <code>across</code> and <code>mutate</code></li>
<li>Choose columns that contain “Pctl” in the name. <strong>Hint</strong>: use <code>contains(&quot;Pctl&quot;)</code> inside <code>across</code>.</li>
<li>Use an anonymous function (“function on the fly”) to divide by 100 (<code>function(x) x / 100</code>).</li>
<li>Check your work - It will also be easier if you <code>select(contains(&quot;Pctl&quot;))</code>.</li>
</ul>
<pre><code># General format
data %&gt;%
mutate(across(
Expand All @@ -229,16 +253,16 @@ <h3>P.2</h3>
<ul>
<li><strong>Hint</strong>: use <code>starts_with()</code> to select the columns that start with “PM”.</li>
<li>Use an anonymous function (“function on the fly”) to do a logical test if the value is greater than 10.</li>
<li>A logical test with <code>mutate</code> will automatically fill a column with TRUE/FALSE.</li>
<li>A logical test with <code>mutate</code> (x &gt; 10) will automatically fill a column with TRUE/FALSE.</li>
</ul>
</div>
<div id="p.3" class="section level3">
<h3>P.3</h3>
<p>Take your code from previous question and assign it to the variable <code>ces_dat</code>.</p>
<ul>
<li>Use <code>filter()</code> to drop any rows where “Oakland” appears in <code>ApproxLocation</code>. Make sure to reassign this to <code>ces_dat</code>.</li>
<li>Create a ggplot boxplot (<code>geom_boxplot()</code>) where (1) the x-axis is <code>Asthma</code> and (2) the y-axis is <code>PM2.5</code>.</li>
<li>You change the <code>labs()</code> layer so that the x-axis is “ER Visits for Asthma: PM2.5 greater than 10”</li>
<li>Create a ggplot where the x-axis is <code>Asthma</code> and the y-axis is <code>PM2.5</code>.</li>
<li>Add a boxplot (<code>geom_boxplot()</code>)</li>
<li>Change the <code>labs()</code> layer so that the x-axis is “ER Visits for Asthma: PM2.5 greater than 10”</li>
</ul>
</div>
</div>
Expand Down

0 comments on commit ffc09e7

Please sign in to comment.