Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand material on if statements #1085

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assignments/R-conditionals.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']
---

Expand Down
62 changes: 59 additions & 3 deletions materials/conditionals-R.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -169,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

Expand Down Expand Up @@ -220,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
Expand Down Expand Up @@ -256,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)

Expand Down
Loading