From 1729264d91252fdd6caeefcbcbe6fbc56e1fb369 Mon Sep 17 00:00:00 2001 From: hadley Date: Thu, 6 Oct 2016 17:40:25 -0500 Subject: [PATCH] More comments from @csgillespie --- functions.Rmd | 14 +++++++------- vectors.Rmd | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/functions.Rmd b/functions.Rmd index 7bfbfa8..65bf7ed 100644 --- a/functions.Rmd +++ b/functions.Rmd @@ -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 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 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. @@ -419,6 +413,12 @@ if (y < 20) { argument that defaults to `lubridate::now()`. That will make it 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? ```{r, eval = FALSE} diff --git a/vectors.Rmd b/vectors.Rmd index 52124cf..ec98c96 100644 --- a/vectors.Rmd +++ b/vectors.Rmd @@ -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. 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 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.