From 04093294b17361a5c8f6a36f5cf37fb4fd75b8f6 Mon Sep 17 00:00:00 2001 From: behrman Date: Fri, 22 Jul 2016 09:22:53 -0700 Subject: [PATCH] Fix typos (#159) --- iteration.Rmd | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/iteration.Rmd b/iteration.Rmd index 6305323..1ff0b11 100644 --- a/iteration.Rmd +++ b/iteration.Rmd @@ -13,9 +13,9 @@ In [functions], we talked about how important it is to reduce duplication in you 1. You're likely to have fewer bugs because each line of code is used in more places. -One part of reducing duplication is writing functions. Functions allow you to identify repeated patterns of code and extract them out into indepdent pieces that you can reuse and easily update as code changes. Iteration helps you when you need to do the same thing to multiple inputs: repeating the same operation on different columns, or on different datasets. (Generally, you won't need to use explicit iteration to deal with different subsets of your data: in most cases the implicit iteration in dplyr will take care of that problem for you.) +One part of reducing duplication is writing functions. Functions allow you to identify repeated patterns of code and extract them out into independent pieces that you can reuse and easily update as code changes. Iteration helps you when you need to do the same thing to multiple inputs: repeating the same operation on different columns, or on different datasets. (Generally, you won't need to use explicit iteration to deal with different subsets of your data: in most cases the implicit iteration in dplyr will take care of that problem for you.) -In this chapter you'll learn about two important iteration paradigms: imperative programming and functional programming, and the machinery each provides. On the imperative side you have things like for loops and while loops, which are a great place to start because they make iteration very explicit, so it's obvious what's happening. However, for loops are quite verbose, and include quite a bit of book-keeping code, that is duplicated for every for loop. Functional programming (FP) offers tools to extract out this duplicated code, so each common for loop pattern gets its own function. Once you master the vocabulary of FP, you can solve many common iteration problems with less code, more ease, and fewer errors. +In this chapter you'll learn about two important iteration paradigms: imperative programming and functional programming, and the machinery each provides. On the imperative side you have things like for loops and while loops, which are a great place to start because they make iteration very explicit, so it's obvious what's happening. However, for loops are quite verbose, and include quite a bit of bookkeeping code, that is duplicated for every for loop. Functional programming (FP) offers tools to extract out this duplicated code, so each common for loop pattern gets its own function. Once you master the vocabulary of FP, you can solve many common iteration problems with less code, more ease, and fewer errors. Some people will tell you to avoid for loops because they are slow. They're wrong! (Well at least they're rather out of date, as for loops haven't been slow for many years). The chief benefits of using FP functions like `lapply()` or `purrr::map()` is that they are more expressive and make code both easier to write and easier to read. @@ -28,7 +28,7 @@ The goal of using purrr functions instead of for loops is to allow you break com solution to every element in the list. 1. If you're solving a complex problem, how can you break it down into - bite sized pieces that allow you to advance one small step towards a + bite-sized pieces that allow you to advance one small step towards a solution? With purrr, you get lots of small pieces that you can compose together with the pipe. @@ -345,7 +345,7 @@ I mention while loops briefly, because I hardly ever use them. They're most ofte 1. Imagine you have a directory full of csv files that you want to read in. You have their paths in a vector, - `files <- dir("data/", pattern = "\\.csv$", full.paths = TRUE)`, and now + `files <- dir("data/", pattern = "\\.csv$", full.names = TRUE)`, and now want to read each one with `read_csv()`. Write the for loop that will load them into a single data frame. @@ -369,7 +369,7 @@ I mention while loops briefly, because I hardly ever use them. They're most ofte trans <- list( disp = function(x) x * 0.0163871, am = function(x) { - factor(x, levels = c("auto", "manual")) + factor(x, labels = c("auto", "manual")) } ) for (var in names(trans)) { @@ -498,7 +498,7 @@ map_dbl(df, median) map_dbl(df, sd) ``` -Compared to using a for loop, focus is on the operation being performed (i.e. `mean()`, `median()`, `sd()`), not the book-keeping required to loop over every element and store the output. This is even more apparent if we use the pipe: +Compared to using a for loop, focus is on the operation being performed (i.e. `mean()`, `median()`, `sd()`), not the bookkeeping required to loop over every element and store the output. This is even more apparent if we use the pipe: ```{r} df %>% map_dbl(mean) @@ -718,10 +718,10 @@ What if you also want to vary the standard deviation? One way to do that would b ```{r} sigma <- list(1, 5, 10) -seq_along(mu) %>% map(~ rnorm(5, mu[[.]], sigma[[.]])) %>% str() +seq_along(mu) %>% map(~rnorm(5, mu[[.]], sigma[[.]])) %>% str() ``` -However, that someone obfuscates the intent of the code. Instead we could use `map2()` which works with iterates over two vectors in parallel: +However, that somewhat obfuscates the intent of the code. Instead we could use `map2()` which works with iterates over two vectors in parallel: ```{r} map2(mu, sigma, rnorm, n = 5) %>% str() @@ -854,7 +854,7 @@ pwalk(list(paths, plots), ggsave, path = tempdir()) ## Other patterns of for loops -Purrr provides a number of other functions that abstract over other types of for loops. You'll use them less frequently than the map functions, but they're useful to have in your back pocket. The goal here is to briefly illustrate each function so hopefully it will come to mind if you see a similar problem in the future. Then you can go look up the documentation for more details. +Purrr provides a number of other functions that abstract over other types of for loops. You'll use them less frequently than the map functions, but they're useful to have in your back pocket. The goal here is to briefly illustrate each function, so hopefully it will come to mind if you see a similar problem in the future. Then you can go look up the documentation for more details. ### Predicate functions