More comments from @csgillespie
This commit is contained in:
parent
7dba596fe2
commit
1729264d91
|
@ -154,12 +154,6 @@ This is an important part of the "do not repeat yourself" (or DRY) principle. Th
|
||||||
write your own functions to compute the variance and skew of a numeric
|
write your own functions to compute the variance and skew of a numeric
|
||||||
vector.
|
vector.
|
||||||
|
|
||||||
1. Implement a `fizzbuzz` function. It takes a single number as input. If
|
|
||||||
the number is divisible by three, it returns "fizz". If it's divisible by
|
|
||||||
five it returns "buzz". If it's divisible by three and five, it returns
|
|
||||||
"fizzbuzz". Otherwise, it returns the number. Make sure you first write
|
|
||||||
working code before you create the function.
|
|
||||||
|
|
||||||
1. Write `both_na()`, a function that takes two vectors of the same length
|
1. Write `both_na()`, a function that takes two vectors of the same length
|
||||||
and returns the number of positions that have an `NA` in both vectors.
|
and returns the number of positions that have an `NA` in both vectors.
|
||||||
|
|
||||||
|
@ -284,7 +278,7 @@ if (condition) {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
To get help on `if` you need to surround it in backticks: `` ?`if` ``.
|
To get help on `if` you need to surround it in backticks: `` ?`if` ``. The help isn't particularly helpful if you're not already an experienced programmer, but at least you know how to get to it!
|
||||||
|
|
||||||
Here's a simple function that uses an if statement. The goal of this function is to return a logical vector describing whether or not each element of a vector is named.
|
Here's a simple function that uses an if statement. The goal of this function is to return a logical vector describing whether or not each element of a vector is named.
|
||||||
|
|
||||||
|
@ -419,6 +413,12 @@ if (y < 20) {
|
||||||
argument that defaults to `lubridate::now()`. That will make it
|
argument that defaults to `lubridate::now()`. That will make it
|
||||||
easier to test your function.)
|
easier to test your function.)
|
||||||
|
|
||||||
|
1. Implement a `fizzbuzz` function. It takes a single number as input. If
|
||||||
|
the number is divisible by three, it returns "fizz". If it's divisible by
|
||||||
|
five it returns "buzz". If it's divisible by three and five, it returns
|
||||||
|
"fizzbuzz". Otherwise, it returns the number. Make sure you first write
|
||||||
|
working code before you create the function.
|
||||||
|
|
||||||
1. How could you use `cut()` to simplify this set of nested if-else statements?
|
1. How could you use `cut()` to simplify this set of nested if-else statements?
|
||||||
|
|
||||||
```{r, eval = FALSE}
|
```{r, eval = FALSE}
|
||||||
|
|
|
@ -149,7 +149,8 @@ Normally you don't need to know about these different types because you can alwa
|
||||||
|
|
||||||
1. Describe the difference between `is.finite(x)` and `!is.infinite(x)`.
|
1. Describe the difference between `is.finite(x)` and `!is.infinite(x)`.
|
||||||
|
|
||||||
1. Read the source code for `dplyr::near()`. How does it work?
|
1. Read the source code for `dplyr::near()` (Hint: to see the source code,
|
||||||
|
drop the `()`). How does it work?
|
||||||
|
|
||||||
1. A logical vector can take 3 possible values. How many possible
|
1. A logical vector can take 3 possible values. How many possible
|
||||||
values can an integer vector take? How many possible values can
|
values can an integer vector take? How many possible values can
|
||||||
|
@ -211,7 +212,7 @@ if (length(x)) {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In this case, 0 is converted to `FALSE` and everything else is converted to `TRUE`. I think this makes it harder to understand your code, and I don't recommend it.
|
In this case, 0 is converted to `FALSE` and everything else is converted to `TRUE`. I think this makes it harder to understand your code, and I don't recommend it. Instead be explicit: `length(x) > 0`.
|
||||||
|
|
||||||
It's also important to understand what happens when you try and create a vector containing multiple types with `c()`: the most complex type always wins.
|
It's also important to understand what happens when you try and create a vector containing multiple types with `c()`: the most complex type always wins.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue