Skip to content

Commit

Permalink
Data masking
Browse files Browse the repository at this point in the history
  • Loading branch information
Nics-Github committed Nov 8, 2023
1 parent e644cc2 commit 6cfd727
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The `congress_age` dataset contains the age of members of the United States cong
library(tidyverse)
library(babynames)
view(babynames)
#view(babynames)
```

## Exercise 1
Expand All @@ -33,10 +33,10 @@ Write a function called `count_name` that, when given a name (e.g.,`Angelica`, `

```{r}
# Write your code below
count_name <- function(nombre = "Nicholas"){
count_name <- function(data = babynames , nombre = "Nicholas"){
if(is.element(nombre, babynames$name)) {
babynames |>
filter(name == nombre ) |>
data |>
filter(name == nombre) |>
group_by(name,year) |>
reframe(year= year, n = sum(n))
Expand All @@ -49,13 +49,14 @@ count_name <- function(nombre = "Nicholas"){
```


## Bonus!

The `count_name` function should return one row per year that matches (and generate an error message if there are no matches). Run the function once with the argument `Ezekiel` and once with `Ezze`.

```{r}
# Paste the code from Exercise 1 below and adjust it
count_name("Ezekiel")
count_name(data=babynames, nombre = "Ezekiel")
#count_name("Ezze")
```

Expand All @@ -67,10 +68,14 @@ Challenge: Add an `else if` after your `if` and before your `else` to catch an e

```{r}
# Write your code below
grab_name <- function(nombre ="Nicholas",ano = 1984){
library(tidyverse)
library(babynames)
grab_name <- function(data = babynames, nombre ,ano ){
if(is.element(nombre,babynames$name)) {
babynames |>
data |>
filter(name == nombre & year == ano) |>
pull(name,year)
}
Expand All @@ -81,7 +86,7 @@ grab_name <- function(nombre ="Nicholas",ano = 1984){
stop("Name and year not found")
}
}
grab_name()
grab_name( nombre = "Samantha", ano = 1950)
```

Expand All @@ -93,13 +98,13 @@ Write a function called `count_name_graph()` that will use the function `countna

```{r}
# Write your code below
count_name_graph <- function(nombre = "Nicholas"){
count_name( nombre )|>
count_name_graph <- function(data, nombre = "Nicholas"){
count_name(data, nombre )|>
ggplot(aes(x=year, y=n))+
geom_line()+
labs(title = paste("Babies named", nombre))
}
count_name_graph("Nicholas")
count_name_graph(data=babynames, nombre = "Nicholas")
```

# Generalizing Functions for Data Frames
Expand Down Expand Up @@ -131,5 +136,5 @@ summary_prop <- function(data, condition){
summary_prop(data=congress_age, condition = age >= 25 & age <= 55)
summary_prop(data=count_name(), condition = n>100)
summary_prop(data=count_name(data = babynames, nombre = "Ezekiel"), condition = n>100)
```
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ <h1>User-defined Functions</h1>
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(babynames)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">view</span>(babynames)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co">#view(babynames)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<section id="exercise-1" class="level2">
<h2 class="anchored" data-anchor-id="exercise-1">Exercise 1</h2>
<p>Write a function called <code>count_name</code> that, when given a name (e.g.,<code>Angelica</code>, <code>Ezekiel</code>, or <code>Riley</code>) as an argument, returns the total number of births by year from the <code>babynames</code> data frame in the <code>babynames</code> package that match that name.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Write your code below</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>count_name <span class="ot">&lt;-</span> <span class="cf">function</span>(<span class="at">nombre =</span> <span class="st">"Nicholas"</span>){</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>count_name <span class="ot">&lt;-</span> <span class="cf">function</span>(<span class="at">data =</span> babynames , <span class="at">nombre =</span> <span class="st">"Nicholas"</span>){</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">is.element</span>(nombre, babynames<span class="sc">$</span>name)) {</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> babynames <span class="sc">|&gt;</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(name <span class="sc">==</span> nombre ) <span class="sc">|&gt;</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> data <span class="sc">|&gt;</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(name <span class="sc">==</span> nombre) <span class="sc">|&gt;</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(name,year) <span class="sc">|&gt;</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">reframe</span>(<span class="at">year=</span> year, <span class="at">n =</span> <span class="fu">sum</span>(n)) </span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> </span>
Expand All @@ -225,7 +225,7 @@ <h2 class="anchored" data-anchor-id="bonus">Bonus!</h2>
<p>The <code>count_name</code> function should return one row per year that matches (and generate an error message if there are no matches). Run the function once with the argument <code>Ezekiel</code> and once with <code>Ezze</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Paste the code from Exercise 1 below and adjust it</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="fu">count_name</span>(<span class="st">"Ezekiel"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="fu">count_name</span>(<span class="at">data=</span>babynames, <span class="at">nombre =</span> <span class="st">"Ezekiel"</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># A tibble: 141 × 3
name year n
Expand All @@ -251,24 +251,28 @@ <h2 class="anchored" data-anchor-id="exercise-2">Exercise 2</h2>
<p>Challenge: Add an <code>else if</code> after your <code>if</code> and before your <code>else</code> to catch an error if people choose a year less than 1880 (outside of the range of our data).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Write your code below</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>grab_name <span class="ot">&lt;-</span> <span class="cf">function</span>(<span class="at">nombre =</span><span class="st">"Nicholas"</span>,<span class="at">ano =</span> <span class="dv">1984</span>){</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">is.element</span>(nombre,babynames<span class="sc">$</span>name)) {</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> babynames <span class="sc">|&gt;</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(name <span class="sc">==</span> nombre <span class="sc">&amp;</span> year <span class="sc">==</span> ano) <span class="sc">|&gt;</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>(name,year)</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span> <span class="cf">if</span>(ano <span class="sc">&lt;</span> <span class="dv">1880</span>){</span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"Choose a year after 1880"</span>)</span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span>{</span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"Name and year not found"</span>)</span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a><span class="fu">grab_name</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(babynames)</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a>grab_name <span class="ot">&lt;-</span> <span class="cf">function</span>(<span class="at">data =</span> babynames, nombre ,ano ){</span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">is.element</span>(nombre,babynames<span class="sc">$</span>name)) {</span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> data <span class="sc">|&gt;</span></span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(name <span class="sc">==</span> nombre <span class="sc">&amp;</span> year <span class="sc">==</span> ano) <span class="sc">|&gt;</span></span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>(name,year)</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span> <span class="cf">if</span>(ano <span class="sc">&lt;</span> <span class="dv">1880</span>){</span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"Choose a year after 1880"</span>)</span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span>{</span>
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"Name and year not found"</span>)</span>
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a><span class="fu">grab_name</span>( <span class="at">nombre =</span> <span class="st">"Samantha"</span>, <span class="at">ano =</span> <span class="dv">1950</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> 1984 1984
"Nicholas" "Nicholas" </code></pre>
<pre><code> 1950
"Samantha" </code></pre>
</div>
</div>
</section>
Expand All @@ -280,13 +284,13 @@ <h2 class="anchored" data-anchor-id="exercise-3">Exercise 3</h2>
<p>Write a function called <code>count_name_graph()</code> that will use the function <code>countname()</code> to make a line graph that plots the year and the number of babies in a given year. The graph’s title should be “the”Babies Named (name of baby)” . The <code>paste()</code> function in r will help with your title, use it to put two strings together. Label your x and y axes.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Write your code below</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>count_name_graph <span class="ot">&lt;-</span> <span class="cf">function</span>(<span class="at">nombre =</span> <span class="st">"Nicholas"</span>){</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count_name</span>( nombre )<span class="sc">|&gt;</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>count_name_graph <span class="ot">&lt;-</span> <span class="cf">function</span>(data, <span class="at">nombre =</span> <span class="st">"Nicholas"</span>){</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count_name</span>(data, nombre )<span class="sc">|&gt;</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>year, <span class="at">y=</span>n))<span class="sc">+</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>()<span class="sc">+</span></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="fu">paste</span>(<span class="st">"Babies named"</span>, nombre))</span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a><span class="fu">count_name_graph</span>(<span class="st">"Nicholas"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a><span class="fu">count_name_graph</span>(<span class="at">data=</span>babynames, <span class="at">nombre =</span> <span class="st">"Nicholas"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="day_25solutions_function_writing_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
</div>
Expand Down Expand Up @@ -323,12 +327,12 @@ <h2 class="anchored" data-anchor-id="exercise-4">Exercise 4</h2>
&lt;int&gt; &lt;dbl&gt;
1 10657 0.572</code></pre>
</div>
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary_prop</span>(<span class="at">data=</span><span class="fu">count_name</span>(), <span class="at">condition =</span> n<span class="sc">&gt;</span><span class="dv">100</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary_prop</span>(<span class="at">data=</span><span class="fu">count_name</span>(<span class="at">data =</span> babynames, <span class="at">nombre =</span> <span class="st">"Ezekiel"</span>), <span class="at">condition =</span> n<span class="sc">&gt;</span><span class="dv">100</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># A tibble: 1 × 2
number_with_condition prop_w_condition
&lt;int&gt; &lt;dbl&gt;
1 201 0.901</code></pre>
1 44 0.312</code></pre>
</div>
</div>

Expand Down
10 changes: 5 additions & 5 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ <h1>Schedule</h1>
</section>
<section id="day-26-iteration" class="level1">
<h1>Day 26 Iteration</h1>
<p><a href="https://forms.gle/239mzry4vtcMCfsv9">Do we need more class time</a></p>
<p><a href="https://forms.gle/239mzry4vtcMCfsv9">Do we need more class time</a>?</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
Expand All @@ -269,15 +269,15 @@ <h1>Day 26 Iteration</h1>
<p>Project 2 notes:</p>
<ul>
<li><p>Project 2 due Friday</p></li>
<li><p>Render your project</p></li>
<li><p>Comment your code in fec_analysis.qmd</p></li>
<li><p><strong>Render</strong> your project</p></li>
<li><p><strong>Comment</strong> your code in <code>fec_analysis.qmd</code></p></li>
<li><p>Make your code readable with spaces and short lines</p></li>
<li><p>filter( == ) vs filter( %in% )</p></li>
<li><p><code>filter( == )</code> vs <code>filter( %in% )</code></p></li>
</ul>
</div>
</div>
</div>
<p><a href="./course-materials/in-class-activies/day_25solutions_writing_functions.qmd">Solution to Monday’s in class activity</a></p>
<p><a href="./course-materials/in-class-activies/day_25solutions_writing_functions.html">Solution to Monday’s in class activity</a></p>
<p><a href="./course-materials/lectures/Day_26_across_maps.html">Lecture: <code>across()</code> and <code>map()</code></a></p>
<p><a href="./course-materials/in-class-activies/day_26_In-classExercise-Iteration.qmd">In class activity</a></p>
<p><a href="https://rlang.r-lib.org/reference/topic-data-mask.html">More on masking</a></p>
Expand Down
Loading

0 comments on commit 6cfd727

Please sign in to comment.