Skip to content

Commit

Permalink
Merge pull request #118 from fhdsl/functions-changes
Browse files Browse the repository at this point in the history
Tweaks and re-render
  • Loading branch information
avahoffman authored Jul 18, 2024
2 parents 967ecf5 + 359dcac commit 977b052
Show file tree
Hide file tree
Showing 8 changed files with 628 additions and 8,554 deletions.
272 changes: 143 additions & 129 deletions modules/Functions/Functions.Rmd

Large diffs are not rendered by default.

694 changes: 407 additions & 287 deletions modules/Functions/Functions.html

Large diffs are not rendered by default.

Binary file modified modules/Functions/Functions.pdf
Binary file not shown.
8,036 changes: 0 additions & 8,036 deletions modules/Functions/clean_data.csv

This file was deleted.

27 changes: 10 additions & 17 deletions modules/Functions/lab/Functions_Lab.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ knitr::opts_chunk$set(echo = TRUE)
Load all the libraries we will use in this lab.

```{r message=FALSE}
library(readr)
library(dplyr)
library(ggplot2)
library(tidyverse)
```

### 1.1

Create a function that takes one argument, a vector, and returns the sum of the vector and squares the result. Call it "sum_squared". Test your function on the vector `c(2,7,21,30,90)` - you should get the answer 22500.
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 `c(2,7,21,30,90)` - you should get the answer 22500.

```
# General format
Expand Down Expand Up @@ -73,9 +71,7 @@ Create a new number `b_num` that is not contained with `nums`. Use your updated
Read in the CalEnviroScreen from https://daseh.org/data/CalEnviroScreen_data.csv. Assign the data the name "ces".

```{r message = FALSE, label = '2.1response'}
ces <- read_csv("https://daseh.org/data/CalEnviroScreen_data.csv")
# If downloaded
# ces <- read_csv("CalEnviroScreen_data.csv")
```

### 2.2
Expand All @@ -86,9 +82,8 @@ We want to get some summary statistics on water contamination. Use `across` insi
# General format
data %>%
summarize(across(
.cols = {vector or tidyselect},
.fns = {some function},
{additional arguments}
{vector or tidyselect},
{some function}
))
```

Expand All @@ -99,14 +94,14 @@ data %>%

### 2.3

Use `across` and `mutate` to convert all columns containing the word "Pctl" into proportions (i.e., divide that value by 100). **Hint**: use `contains()` to select the right columns within `across()`. Use a "function on the fly" to divide by 100 (`function(x) x / 100`). It will also be easier to check your work if you `select()` columns that match "Pctl".
Use `across` and `mutate` to convert all columns containing the word "Pctl" into proportions (i.e., divide that value by 100). **Hint**: use `contains()` to select the right columns within `across()`. Use an anonymous function ("function on the fly") to divide by 100 (`function(x) x / 100`). It will also be easier to check your work if you `select()` columns that match "Pctl".

```
# General format
data %>%
mutate(across(
.cols = {vector or tidyselect},
.fns = {some function}
{vector or tidyselect},
{some function}
))
```

Expand All @@ -118,16 +113,14 @@ data %>%

### P.2



Use `across` and `mutate` to convert all columns starting with the string "PM" into a binary variable: TRUE if the value is greater than 10 and FALSE if less than or equal to 10. **Hint**: use `starts_with()` to select the columns that start with "PM". Use a "function on the fly" to do a logical test if the value is greater than 10.
Use `across` and `mutate` to convert all columns starting with the string "PM" into a binary variable: TRUE if the value is greater than 10 and FALSE if less than or equal to 10. **Hint**: use `starts_with()` to select the columns that start with "PM". Use an anonymous function ("function on the fly") to do a logical test if the value is greater than 10.

```{r P.2response}
```


### P.3
### P.3

Take your code from question 2.4 and assign it to the variable `ces_dat`.

Expand Down
56 changes: 30 additions & 26 deletions modules/Functions/lab/Functions_Lab.html
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,13 @@ <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>
<pre class="r"><code>library(readr)
library(dplyr)
library(ggplot2)</code></pre>
<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 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>
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>
Expand All @@ -388,8 +386,8 @@ <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="section-3" class="section level3">
<h3>1.4</h3>
<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
Expand All @@ -398,12 +396,12 @@ <h3>1.4</h3>
</div>
<div id="part-2" class="section level1">
<h1>Part 2</h1>
<div id="section-4" class="section level3">
<div id="section-3" class="section level3">
<h3>2.1</h3>
<p>Read in the CalEnviroScreen from <a href="https://daseh.org/data/CalEnviroScreen_data.csv" class="uri">https://daseh.org/data/CalEnviroScreen_data.csv</a>. Assign
the data the name “ces”.</p>
</div>
<div id="section-5" class="section level3">
<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
Expand All @@ -415,34 +413,40 @@ <h3>2.2</h3>
<pre><code># General format
data %&gt;%
summarize(across(
.cols = {vector or tidyselect},
.fns = {some function},
{additional arguments}
{vector or tidyselect},
{some function}
))</code></pre>
</div>
<div id="section-6" class="section level3">
<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 “Pctl” 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 a “function
on the fly” to divide by 100. It will also be easier to check your work
if you <code>select()</code> columns that match “Pctl”.</p>
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>
<pre><code># General format
data %&gt;%
mutate(across(
{vector or tidyselect},
{some function}
))</code></pre>
</div>
</div>
<div id="section-7" class="section level3">
<h3>2.4</h3>
<div id="practice-on-your-own" class="section level1">
<h1>Practice on Your Own!</h1>
<div id="p.2" class="section level3">
<h3>P.2</h3>
<p>Use <code>across</code> and <code>mutate</code> to convert all
columns starting with the string “PM” into a binary variable: TRUE if
the value is greater than 10 and FALSE if less than or equal to 10.
<strong>Hint</strong>: use <code>starts_with()</code> to select the
columns that start with “PM”. Use a “function on the fly” to do a
logical test if the value is greater than 10.</p>
</div>
columns that start with “PM”. Use an anonymous function (“function on
the fly”) to do a logical test if the value is greater than 10.</p>
</div>
<div id="practice-on-your-own" class="section level1">
<h1>Practice on Your Own!</h1>
<div id="p.1" class="section level3">
<h3>P.1</h3>
<div id="p.3" class="section level3">
<h3>P.3</h3>
<p>Take your code from question 2.4 and assign it to the variable
<code>ces_dat</code>.</p>
<ul>
Expand Down
40 changes: 17 additions & 23 deletions modules/Functions/lab/Functions_Lab_Key.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ knitr::opts_chunk$set(echo = TRUE)
Load all the libraries we will use in this lab.

```{r message=FALSE}
library(readr)
library(dplyr)
library(ggplot2)
library(tidyverse)
```

### 1.1
Expand Down Expand Up @@ -104,45 +102,43 @@ We want to get some summary statistics on water contamination. Use `across` insi
# General format
data %>%
summarize(across(
.cols = {vector or tidyselect},
.fns = {some function},
{additional arguments}
{vector or tidyselect},
{some function}
))
```

```{r 2.2response}
ces %>%
summarize(across(
.cols = contains("Water") & ends_with("Pctl"),
.fns = sum
contains("Water") & ends_with("Pctl"),
sum
))
ces %>%
summarize(across(
.cols = contains("Water") & ends_with("Pctl"),
.fns = sum,
na.rm = TRUE
contains("Water") & ends_with("Pctl"),
function(x) sum(x, na.rm = T)
))
```


### 2.3

Use `across` and `mutate` to convert all columns containing the word "Pctl" into proportions (i.e., divide that value by 100). **Hint**: use `contains()` to select the right columns within `across()`. Use a "function on the fly" to divide by 100 (`function(x) x / 100`). It will also be easier to check your work if you `select()` columns that match "Pctl".
Use `across` and `mutate` to convert all columns containing the word "Pctl" into proportions (i.e., divide that value by 100). **Hint**: use `contains()` to select the right columns within `across()`. Use an anonymous function ("function on the fly") to divide by 100 (`function(x) x / 100`). It will also be easier to check your work if you `select()` columns that match "Pctl".

```
# General format
data %>%
mutate(across(
.cols = {vector or tidyselect},
.fns = {some function}
{vector or tidyselect},
{some function}
))
```

```{r 2.3response}
ces %>%
mutate(across(
.cols = contains("Pctl"),
.fns = function(x) x / 100
contains("Pctl"),
function(x) x / 100
)) %>%
select(contains("Pctl"))
```
Expand All @@ -151,15 +147,13 @@ ces %>%

### P.2



Use `across` and `mutate` to convert all columns starting with the string "PM" into a binary variable: TRUE if the value is greater than 10 and FALSE if less than or equal to 10. **Hint**: use `starts_with()` to select the columns that start with "PM". Use a "function on the fly" to do a logical test if the value is greater than 10.
Use `across` and `mutate` to convert all columns starting with the string "PM" into a binary variable: TRUE if the value is greater than 10 and FALSE if less than or equal to 10. **Hint**: use `starts_with()` to select the columns that start with "PM". Use an anonymous function ("function on the fly") to do a logical test if the value is greater than 10.

```{r P.2response}
ces %>%
mutate(across(
.cols = starts_with("PM"),
.fns = function(x) x > 10
starts_with("PM"),
function(x) x > 10
))
```

Expand All @@ -176,8 +170,8 @@ Take your code from question 2.4 and assign it to the variable `ces_dat`.
ces_dat <-
ces %>%
mutate(across(
.cols = starts_with("PM"),
.fns = function(x) x > 10
starts_with("PM"),
function(x) x > 10
)) %>%
filter(ApproxLocation != "Oakland")
Expand Down
57 changes: 21 additions & 36 deletions modules/Functions/lab/Functions_Lab_Key.html
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,7 @@ <h1 class="title toc-ignore">Functions Lab - Key</h1>
<div id="part-1" class="section level1">
<h1>Part 1</h1>
<p>Load all the libraries we will use in this lab.</p>
<pre class="r"><code>library(readr)
library(dplyr)
library(ggplot2)</code></pre>
<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
Expand Down Expand Up @@ -444,36 +442,23 @@ <h3>2.2</h3>
<pre><code># General format
data %&gt;%
summarize(across(
.cols = {vector or tidyselect},
.fns = {some function},
{additional arguments}
{vector or tidyselect},
{some function}
))</code></pre>
<pre class="r"><code>ces %&gt;%
summarize(across(
.cols = contains(&quot;Water&quot;) &amp; ends_with(&quot;Pctl&quot;),
.fns = sum
contains(&quot;Water&quot;) &amp; ends_with(&quot;Pctl&quot;),
sum
))</code></pre>
<pre><code>## # A tibble: 1 × 3
## DrinkingWaterPctl GroundwaterThreatsPctl ImpWaterBodiesPctl
## &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
## 1 NA 304029. 256802.</code></pre>
<pre class="r"><code>ces %&gt;%
summarize(across(
.cols = contains(&quot;Water&quot;) &amp; ends_with(&quot;Pctl&quot;),
.fns = sum,
na.rm = TRUE
contains(&quot;Water&quot;) &amp; ends_with(&quot;Pctl&quot;),
function(x) sum(x, na.rm = T)
))</code></pre>
<pre><code>## Warning: There was 1 warning in `summarize()`.
## ℹ In argument: `across(...)`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
##
## # Previously
## across(a:b, mean, na.rm = TRUE)
##
## # Now
## across(a:b, \(x) mean(x, na.rm = TRUE))</code></pre>
<pre><code>## # A tibble: 1 × 3
## DrinkingWaterPctl GroundwaterThreatsPctl ImpWaterBodiesPctl
## &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
Expand All @@ -484,20 +469,20 @@ <h3>2.3</h3>
<p>Use <code>across</code> and <code>mutate</code> to convert all
columns containing the word “Pctl” 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 a “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>
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>
<pre><code># General format
data %&gt;%
mutate(across(
.cols = {vector or tidyselect},
.fns = {some function}
{vector or tidyselect},
{some function}
))</code></pre>
<pre class="r"><code>ces %&gt;%
mutate(across(
.cols = contains(&quot;Pctl&quot;),
.fns = function(x) x / 100
contains(&quot;Pctl&quot;),
function(x) x / 100
)) %&gt;%
select(contains(&quot;Pctl&quot;))</code></pre>
<pre><code>## # A tibble: 8,035 × 23
Expand Down Expand Up @@ -530,12 +515,12 @@ <h3>P.2</h3>
columns starting with the string “PM” into a binary variable: TRUE if
the value is greater than 10 and FALSE if less than or equal to 10.
<strong>Hint</strong>: use <code>starts_with()</code> to select the
columns that start with “PM”. Use a “function on the fly” to do a
logical test if the value is greater than 10.</p>
columns that start with “PM”. Use an anonymous function (“function on
the fly”) to do a logical test if the value is greater than 10.</p>
<pre class="r"><code>ces %&gt;%
mutate(across(
.cols = starts_with(&quot;PM&quot;),
.fns = function(x) x &gt; 10
starts_with(&quot;PM&quot;),
function(x) x &gt; 10
))</code></pre>
<pre><code>## # A tibble: 8,035 × 68
## ...1 CensusTract CaliforniaCounty ZIP Longitude Latitude ApproxLocation
Expand Down Expand Up @@ -575,8 +560,8 @@ <h3>P.3</h3>
<pre class="r"><code>ces_dat &lt;-
ces %&gt;%
mutate(across(
.cols = starts_with(&quot;PM&quot;),
.fns = function(x) x &gt; 10
starts_with(&quot;PM&quot;),
function(x) x &gt; 10
)) %&gt;%
filter(ApproxLocation != &quot;Oakland&quot;)

Expand Down

0 comments on commit 977b052

Please sign in to comment.