Skip to content

Commit

Permalink
Working on functions chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
jrnold committed Jan 9, 2017
1 parent 0088e01 commit 094fd02
Show file tree
Hide file tree
Showing 43 changed files with 5,785 additions and 2,871 deletions.
12 changes: 6 additions & 6 deletions _bookdown.yml
Expand Up @@ -5,15 +5,15 @@ rmd_files: [
"index.rmd",
# "intro.Rmd",

# "explore.Rmd",
"explore.Rmd",
"visualize.Rmd",
"workflow-basics.Rmd",
"transform.Rmd",
# "workflow-scripts.Rmd",
"EDA.Rmd",
# "workflow-projects.Rmd",

# "wrangle.Rmd",
"wrangle.Rmd",
"tibble.Rmd",
"import.Rmd",
"tidy.Rmd",
Expand All @@ -22,8 +22,8 @@ rmd_files: [
"factors.Rmd",
"datetimes.Rmd",

# "program.Rmd",
# "pipes.Rmd",
"program.Rmd",
"pipes.Rmd",
# "functions.Rmd",
"vectors.Rmd",
"iteration.Rmd",
Expand All @@ -33,10 +33,10 @@ rmd_files: [
#"model-building.Rmd",
#"model-many.Rmd",

#"communicate.Rmd",
"communicate.Rmd",
"rmarkdown.Rmd",
#"communicate-plots.Rmd",
#"rmarkdown-formats.Rmd",
"rmarkdown-formats.Rmd",
"rmarkdown-workflow.Rmd",
]

Expand Down
Binary file modified _main.rds
Binary file not shown.
3 changes: 3 additions & 0 deletions communicate.Rmd
@@ -0,0 +1,3 @@
# (PART) Communicate {-}

# Introduction {#communicate-intro}
403 changes: 403 additions & 0 deletions docs/communicate-intro.html

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/communicate.md
@@ -0,0 +1,4 @@

# (PART) Communicate {-}

# Introduction {#communicate-intro}
332 changes: 171 additions & 161 deletions docs/data-import.html

Large diffs are not rendered by default.

342 changes: 176 additions & 166 deletions docs/data-transformation.html

Large diffs are not rendered by default.

349 changes: 181 additions & 168 deletions docs/dates-and-times.html

Large diffs are not rendered by default.

346 changes: 180 additions & 166 deletions docs/exploratory-data-analysis.html

Large diffs are not rendered by default.

404 changes: 404 additions & 0 deletions docs/explore-intro.html

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/explore.md
@@ -0,0 +1,6 @@

# (PART) Explore {-}

# Introduction {#explore-intro}

No exercises.
336 changes: 173 additions & 163 deletions docs/factors.html

Large diffs are not rendered by default.

325 changes: 169 additions & 156 deletions docs/index.html

Large diffs are not rendered by default.

389 changes: 198 additions & 191 deletions docs/iteration.html

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions docs/iteration.md
Expand Up @@ -25,6 +25,12 @@
library("tidyverse")
library("stringr")
```
The package **microbenchmark** is used for timing code

```r
library("microbenchmark")
```


## For Loops

Expand Down Expand Up @@ -434,7 +440,6 @@ Microbenchmark will run an R expression a number of times and time it.
Define a function that appends to an integer vector.

```r
library("microbenchmark")
add_to_vector <- function(n) {
output <- vector("integer", 0)
for (i in seq_len(n)) {
Expand All @@ -445,7 +450,7 @@ add_to_vector <- function(n) {
microbenchmark(add_to_vector(10000), times = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> add_to_vector(10000) 156 158 172 159 180 201 3
#> add_to_vector(10000) 185 196 201 206 209 211 3
```

And one that pre-allocates it.
Expand All @@ -460,8 +465,8 @@ add_to_vector_2 <- function(n) {
}
microbenchmark(add_to_vector_2(10000), times = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> add_to_vector_2(10000) 7.1 7.25 7.33 7.39 7.44 7.49 3
#> expr min lq mean median uq max neval
#> add_to_vector_2(10000) 7.05 7.14 8.02 7.23 8.5 9.77 3
```

The pre-allocated vector is about **100** times faster!
Expand All @@ -484,7 +489,7 @@ This creates a list of data frames.
I then use `bind_rows` to create a single data frame from the list of data frames.

```r
df <- vector("list", lenght(files))
df <- vector("list", length(files))
for (fname in seq_along(files)) {
df[[i]] <- read_csv(files[[i]])
}
Expand Down Expand Up @@ -607,6 +612,9 @@ trans <- list(
factor(x, labels = c("auto", "manual"))
}
)
```

```r
for (var in names(trans)) {
mtcars[[var]] <- trans[[var]](mtcars[[var]])
}
Expand All @@ -624,21 +632,15 @@ E.g. this is a function:

```r
trans[["disp"]]
#> function(x) x * 0.0163871
```
This applies the function to the column of `mtcars` with the same name

```r
trans[["disp"]](mtcars[["disp"]])
#> [1] 0.0430 0.0430 0.0290 0.0693 0.0967 0.0604 0.0967 0.0394 0.0378 0.0450
#> [11] 0.0450 0.0741 0.0741 0.0741 0.1267 0.1235 0.1182 0.0211 0.0203 0.0191
#> [21] 0.0323 0.0854 0.0816 0.0940 0.1074 0.0212 0.0323 0.0255 0.0943 0.0389
#> [31] 0.0808 0.0325
```




## For loops vs. functionals


Expand Down Expand Up @@ -714,10 +716,8 @@ The mean of every column in `mtcars`:

```r
map_dbl(mtcars, mean)
#> Warning in mean.default(.x[[i]], ...): argument is not numeric or logical:
#> returning NA
#> mpg cyl disp hp drat wt qsec vs am
#> 20.091 6.188 3.781 146.688 3.597 3.217 17.849 0.438 NA
#> 20.091 6.188 230.722 146.688 3.597 3.217 17.849 0.438 0.406
#> gear carb
#> 3.688 2.812
```
Expand Down Expand Up @@ -833,7 +833,7 @@ Use `map_lgl` with the function `is.factor`,
```r
map_lgl(mtcars, is.factor)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
#> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
```


Expand Down
351 changes: 182 additions & 169 deletions docs/model-basics.html

Large diffs are not rendered by default.

406 changes: 406 additions & 0 deletions docs/model-intro.html

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion docs/model.md
@@ -1,5 +1,7 @@

# Model Introduction
# (PART) Model {-}

# Introduction {#model-intro}

Some of the discussion of models is slightly different, and has a different emphasis than in most social science research.
This is largely because this book is speaking to data scientists, where the primary goal is prediction rather than theory testing (that I don't view these as too different is a different story).
Expand Down
404 changes: 404 additions & 0 deletions docs/pipes.html

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/pipes.md
@@ -0,0 +1,4 @@

# Pipes

No exercises in this chapter.

0 comments on commit 094fd02

Please sign in to comment.