Skip to content

Commit

Permalink
adding most of the rendered versions
Browse files Browse the repository at this point in the history
  • Loading branch information
carriewright11 committed Jul 17, 2024
1 parent bdeaf4a commit 34bc5b8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 62 deletions.
60 changes: 24 additions & 36 deletions modules/Functions/Functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -3117,7 +3117,7 @@
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
Expand Down Expand Up @@ -3473,45 +3473,10 @@ <h1 data-config-title><!-- populated from slide_config.json --></h1>

</article></slide><slide class=""><hgroup><h2>Functions for tibbles</h2></hgroup><article id="functions-for-tibbles-1">

<p><code>select(n)</code> will choose column <code>n</code>:</p>

<pre class = 'prettyprint lang-r'>get_index &lt;- function(dat, row, col) {
dat %&gt;%
filter(row_number() == row) %&gt;%
select(all_of(col))
}

get_index(dat = ces, row = 10, col = 7)</pre>

<pre ># A tibble: 1 × 1
CES4.0Score
&lt;dbl&gt;
1 43.7</pre>

</article></slide><slide class=""><hgroup><h2>Functions for tibbles</h2></hgroup><article id="functions-for-tibbles-2">

<p>Including default values for arguments:</p>

<pre class = 'prettyprint lang-r'>get_top &lt;- function(dat, row = 1, col = 1) {
dat %&gt;%
filter(row_number() == row) %&gt;%
select(all_of(col))
}

get_top(dat = ces)</pre>

<pre ># A tibble: 1 × 1
CensusTract
&lt;dbl&gt;
1 6001400100</pre>

</article></slide><slide class=""><hgroup><h2>Functions for tibbles</h2></hgroup><article id="functions-for-tibbles-3">

<p>Can create function with an argument that allows inputting a column name for <code>select</code> or other <code>dplyr</code> operation:</p>

<pre class = 'prettyprint lang-r'>clean_dataset &lt;- function(dataset, col_name) {
my_data_out &lt;- dataset %&gt;% select({{col_name}}) # Note the curly braces
write_csv(my_data_out, &quot;clean_data.csv&quot;)
return(my_data_out)
}

Expand All @@ -3532,6 +3497,27 @@ <h1 data-config-title><!-- populated from slide_config.json --></h1>
10 43.7
# ℹ 8,025 more rows</pre>

<pre class = 'prettyprint lang-r'>get_mean &lt;- function(dat, county_name, col_name) {
my_data_out &lt;- dat %&gt;%
filter(str_detect(CaliforniaCounty, county_name)) %&gt;%
summarise(mean = mean({{col_name}}, na.rm = TRUE))
return(my_data_out)
}

get_mean(dat = ces, county_name = &quot;Alameda&quot;, col_name = CES4.0Score)</pre>

<pre ># A tibble: 1 × 1
mean
&lt;dbl&gt;
1 22.9</pre>

<pre class = 'prettyprint lang-r'>get_mean(dat = ces, county_name = &quot;Fresno&quot;, col_name = CES4.0Score)</pre>

<pre ># A tibble: 1 × 1
mean
&lt;dbl&gt;
1 40.9</pre>

</article></slide><slide class=""><hgroup><h2>Summary</h2></hgroup><article id="summary">

<ul>
Expand Down Expand Up @@ -3775,6 +3761,8 @@ <h1 data-config-title><!-- populated from slide_config.json --></h1>

<p>Combining with <code>mutate()</code> - the <code>replace_na</code> function</p>

<p>Here we will use the <code>yearly_co2_emissions</code> data from <code>dasehr</code></p>

<p><code>replace_na({data frame}, {list of values})</code> or <code>replace_na({vector}, {single value})</code></p>

<pre class = 'prettyprint lang-r'>yearly_co2_emissions %&gt;%
Expand Down
8 changes: 4 additions & 4 deletions modules/Functions/lab/Functions_Lab.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,25 @@ data %>%

# Practice on Your Own!

### P.1
### 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.

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


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

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

- use `filter()` to drop any rows where "Oakland" appears in `ApproxLocation`. Make sure to reassign this to `ces_dat`.
- Create a ggplot boxplot (`geom_boxplot()`) where (1) the x-axis is `PM2.5` and (2) the y-axis is `Asthma`.
- You change the `labs()` layer so that the x-axis is "ER Visits for Asthma: PM2.5 greater than 10"

```{r P.2response}
```{r P.3response}
```
8 changes: 4 additions & 4 deletions modules/Functions/lab/Functions_Lab_Key.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ ces %>%

# Practice on Your Own!

### P.1
### 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.

```{r P.1response}
```{r P.2response}
ces %>%
mutate(across(
.cols = starts_with("PM"),
Expand All @@ -164,15 +164,15 @@ ces %>%
```


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

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

- use `filter()` to drop any rows where "Oakland" appears in `ApproxLocation`. Make sure to reassign this to `ces_dat`.
- Create a ggplot boxplot (`geom_boxplot()`) where (1) the x-axis is `PM2.5` and (2) the y-axis is `Asthma`.
- You change the `labs()` layer so that the x-axis is "ER Visits for Asthma: PM2.5 greater than 10"

```{r P.2response}
```{r P.3response}
ces_dat <-
ces %>%
mutate(across(
Expand Down
Loading

0 comments on commit 34bc5b8

Please sign in to comment.