From 2aacdde472889a871b4bc34170edcb18ace2dc52 Mon Sep 17 00:00:00 2001 From: Ethan White Date: Fri, 11 Oct 2024 10:08:09 -0400 Subject: [PATCH 1/5] Change assignment order to align with material Moves DNA or RNA back since boolean functions is taught last --- assignments/R-conditionals.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/R-conditionals.md b/assignments/R-conditionals.md index e0525389..0d6ed001 100644 --- a/assignments/R-conditionals.md +++ b/assignments/R-conditionals.md @@ -3,7 +3,7 @@ layout: page element: assignment title: Making Choices language: R -exercises: ['Choice Operators', 'Basic If Statements', 'If Statements In Functions', 'DNA or RNA', 'Size Estimates by Name', 'Check That Your Code Runs', 'Load or Download File', 'Unit Conversion Challenge'] +exercises: ['Choice Operators', 'Basic If Statements', 'If Statements In Functions', 'Size Estimates by Name', 'DNA or RNA', 'Check That Your Code Runs', 'Load or Download File', 'Unit Conversion Challenge'] points: [10, 20, 20, 20, 20, 10, 'Challenge - optional', 'Challenge - optional'] --- From 6986b84c1e59170e9a8df09ea57931b55b956d87 Mon Sep 17 00:00:00 2001 From: Ethan White Date: Fri, 11 Oct 2024 10:09:17 -0400 Subject: [PATCH 2/5] Add full discussion of vectors in conditionals Adds treatment of elementwise comparison and warning about elementwise comparisons with non-equal length vectors --- materials/conditionals-R.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/materials/conditionals-R.md b/materials/conditionals-R.md index 5983c549..12ddf901 100644 --- a/materials/conditionals-R.md +++ b/materials/conditionals-R.md @@ -132,6 +132,40 @@ counts[c(TRUE, TRUE, FALSE, FALSE)] * This keeps the first and second values in `counts` because the values in the vector are `TRUE` * This is how `dplyr::filter()` and other methods for subsetting data work +* Can also compare element wise to see which values in two vectors are the same + +```r +recounts <- c(9, 16, NA, 12) +counts == recounts +``` + +* Cannot compare none equal length vectors (or at least you shouldn't) +* Will error in cases where the lengths of the vectors aren't even multiples + +```r +states == c("SC", "GA", "FL") +``` + +* Or will repeat the shorter vector + +```r +states == c("GA", "SC") +``` + +* Looks like it works, but this is actually doing an element wise comparison + +```r +states == c("GA", "SC", "GA", "SC") +``` + +* If we change the order we can a different answer + +```r +states == c("SC", "GA") +``` + +* This is why we have `%in%` + > Do [Choice Operators]({{ site.baseurl }}/exercises/Making-choices-choice-operators-R). ### `if` statements From d3d830defd524f6b2d720f7f52b953c5d5a6871e Mon Sep 17 00:00:00 2001 From: Ethan White Date: Fri, 11 Oct 2024 10:10:08 -0400 Subject: [PATCH 3/5] Explain that there are no returns for conditionals This is a common mistake resulting from just learning functions and the braces --- materials/conditionals-R.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/materials/conditionals-R.md b/materials/conditionals-R.md index 12ddf901..ad1d144b 100644 --- a/materials/conditionals-R.md +++ b/materials/conditionals-R.md @@ -203,8 +203,10 @@ x * `x > 5` is `FALSE`, so the code in the `if` doesn't run * `x` is still 4 -* This is *not* a function, so everything that happens in the if statement - influences the global environment +* This is *not* a function +* Everything that happens in the if statement influences the global environment (unless the if statement is in a function) +* No `return()` +* Repeat after me: *return, is only, for functions* * Different mass calculations for different vegetation types From b866b44b5f87522b418a853b03898fbfc669c1a2 Mon Sep 17 00:00:00 2001 From: Ethan White Date: Fri, 11 Oct 2024 10:12:25 -0400 Subject: [PATCH 4/5] Explain that if states need a single logical value Can't pass them vectors. Avoids early confusion and sets up repeating things material --- materials/conditionals-R.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/materials/conditionals-R.md b/materials/conditionals-R.md index ad1d144b..e24c3701 100644 --- a/materials/conditionals-R.md +++ b/materials/conditionals-R.md @@ -256,6 +256,25 @@ mass > Do Tasks 2-3 in [Basic If Statements]({{ site.baseurl }}/exercises/Making-choices-basic-if-statements-R). +#### `if` statements need a single logical value + +* While conditional statements can handle vectors, `if` statements typically can't +* The if condition needs to return a single logical value +* So if we try to pass a vector we'll get an error + +```r +veg_type <- c("tree", "shrub") +volume <- c(16.08, 2.26) +if (veg_type == "shrub") { + mass <- 2.65 * volume^0.9 +} else if (veg_type == "grass") { + mass <- 0.65 * volume^1.2 +} else { + mass <- NA +} +mass +``` + ### Using Conditionals Inside Functions * We've used a conditional to estimate mass differently for different types of vegetation From 77350c60079e2aeedb848ded2e3e1e4d2b8e7cd2 Mon Sep 17 00:00:00 2001 From: Ethan White Date: Fri, 11 Oct 2024 10:12:51 -0400 Subject: [PATCH 5/5] Add additional in-lesson exercises --- materials/conditionals-R.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/materials/conditionals-R.md b/materials/conditionals-R.md index e24c3701..dad07a29 100644 --- a/materials/conditionals-R.md +++ b/materials/conditionals-R.md @@ -311,7 +311,8 @@ est_mass(24, "tree") * It assigns `NA` to mass * It then finishes the if/else if/else statement and returns the value for `mass`, which is `NA` to the global environment -> Do Tasks 4 in [Basic If Statements]({{ site.baseurl }}/exercises/Making-choices-basic-if-statements-R). +> Do Task 4 in [Basic If Statements]({{ site.baseurl }}/exercises/Making-choices-basic-if-statements-R). +> Do Tasks 1 & 2 in [If Statements In Functions]({{ site.baseurl }}/exercises/Making-choices-if-statements-in-functions-R). ### Automatically extracting functions (optional)