Skip to content

Commit

Permalink
Work on ex02
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Apr 2, 2018
1 parent 3e5a1f5 commit 6bfd877
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 44 deletions.
45 changes: 29 additions & 16 deletions ex02_create-or-mutate-in-place.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' ---
#' title: "How to add or modify a variable"
#' title: "Add or modify a variable"
#' author: "Jenny Bryan"
#' date: "`r format(Sys.Date())`"
#' output: github_document
Expand All @@ -18,51 +18,64 @@ options(tidyverse.quiet = TRUE)
library(tidyverse)

# ----
#' ## Function to give my example data frame
#' ### Function to produce a fresh example data frame
new_df <- function() {
tribble(
~ name, ~ age,
"Reed", 14,
"Wesley", 12,
"Eli", 12,
"Toby", 1
"Reed", 14L,
"Wesley", 12L,
"Eli", 12L,
"Toby", 1L
)
}

# ----
#' ## The `df$var <- ...` syntax

#' This absolutely works. But there are downsides:
#' How to create or modify a variable is a fairly low stakes matter, i.e. really
#' a matter of taste. This is not a hill I plan to die on. But here's my two
#' cents.
#'
#' Of course, `df$var <- ...` absolutely works for creating new variables or
#' modifying existing ones. But there are downsides:
#'
#' * Silent recycling is a risk.
#' * `df` is not special. It's not the implied place to look first for things,
#' so you must be explicit.
#' so you must be explicit. This can be a drag.
#' * I have aesthetic concerns. YMMV.
df <- new_df()
df$legs <- 4
df$eyes <- 2L
df$snack <- c("chips", "cheese")
df$uname <- toupper(df$name)
df

# ----
#' ## `dplyr::mutate()` works "inside the box"

#' `dplyr::mutate()` is the tidyverse way to work on a variable:
#' `dplyr::mutate()` is the tidyverse way to work on a variable. If I'm working
#' in a script-y style and the tidyverse packages are already available, I
#' generally prefer this method of adding or modifying a variable.
#'
#' * Only a length one input can be recycled.
#' * `df` is the first place to look for things. It turns out that making a
#' new variable out of existing variables is very, very common.
#' new variable out of existing variables is very, very common, so it's nice
#' when this is easy.
#' * This is pipe-friendly, so I can easily combine with a few other logical
#' data manipuluations that need to happen around the same point.
#' * I like the way this looks. YMMV.
df <- new_df()
df %>%

new_df() %>%
mutate(
legs = 4,
eyes = 2L,
snack = c("chips", "cheese"),
uname = toupper(name)
)
df %>%

#' Oops! I did not provide enough snacks!

new_df() %>%
mutate(
legs = 4,
eyes = 2L,
snack = c("chips", "cheese", "mixed nuts", "nerf bullets"),
uname = toupper(name)
)
72 changes: 44 additions & 28 deletions ex02_create-or-mutate-in-place.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
How to add or modify a variable
Add or modify a variable
================
Jenny Bryan
2018-04-02
Expand All @@ -7,77 +7,93 @@ Jenny Bryan
library(tidyverse)
```

## Function to give my example data frame
### Function to produce a fresh example data frame

``` r
new_df <- function() {
tribble(
~ name, ~ age,
"Reed", 14,
"Wesley", 12,
"Eli", 12,
"Toby", 1
"Reed", 14L,
"Wesley", 12L,
"Eli", 12L,
"Toby", 1L
)
}
```

## The `df$var <- ...` syntax

This absolutely works. But there are downsides:
How to create or modify a variable is a fairly low stakes matter,
i.e. really a matter of taste. This is not a hill I plan to die on. But
here’s my two cents.

Of course, `df$var <- ...` absolutely works for creating new variables
or modifying existing ones. But there are downsides:

- Silent recycling is a risk.
- `df` is not special. It’s not the implied place to look first for
things, so you must be explicit.
things, so you must be explicit. This can be a drag.
- I have aesthetic concerns. YMMV.

<!-- end list -->

``` r
df <- new_df()
df$legs <- 4
df$eyes <- 2L
df$snack <- c("chips", "cheese")
df$uname <- toupper(df$name)
df
#> # A tibble: 4 x 5
#> name age legs snack uname
#> <chr> <dbl> <dbl> <chr> <chr>
#> 1 Reed 14. 4. chips REED
#> 2 Wesley 12. 4. cheese WESLEY
#> 3 Eli 12. 4. chips ELI
#> 4 Toby 1. 4. cheese TOBY
#> name age eyes snack uname
#> <chr> <int> <int> <chr> <chr>
#> 1 Reed 14 2 chips REED
#> 2 Wesley 12 2 cheese WESLEY
#> 3 Eli 12 2 chips ELI
#> 4 Toby 1 2 cheese TOBY
```

## `dplyr::mutate()` works “inside the box”

`dplyr::mutate()` is the tidyverse way to work on a variable:
`dplyr::mutate()` is the tidyverse way to work on a variable. If I’m
working in a script-y style and the tidyverse packages are already
available, I generally prefer this method of adding or modifying a
variable.

- Only a length one input can be recycled.
- `df` is the first place to look for things. It turns out that making
a new variable out of existing variables is very, very common.
a new variable out of existing variables is very, very common, so
it’s nice when this is easy.
- This is pipe-friendly, so I can easily combine with a few other
logical data manipuluations that need to happen around the same
point.
- I like the way this looks. YMMV.

<!-- end list -->

``` r
df <- new_df()
df %>%
new_df() %>%
mutate(
legs = 4,
eyes = 2L,
snack = c("chips", "cheese"),
uname = toupper(name)
)
#> Error in mutate_impl(.data, dots): Column `snack` must be length 4 (the number of rows) or one, not 2
df %>%
```

Oops\! I did not provide enough snacks\!

``` r
new_df() %>%
mutate(
legs = 4,
eyes = 2L,
snack = c("chips", "cheese", "mixed nuts", "nerf bullets"),
uname = toupper(name)
)
#> # A tibble: 4 x 5
#> name age legs snack uname
#> <chr> <dbl> <dbl> <chr> <chr>
#> 1 Reed 14. 4. chips REED
#> 2 Wesley 12. 4. cheese WESLEY
#> 3 Eli 12. 4. mixed nuts ELI
#> 4 Toby 1. 4. nerf bullets TOBY
#> name age eyes snack uname
#> <chr> <int> <int> <chr> <chr>
#> 1 Reed 14 2 chips REED
#> 2 Wesley 12 2 cheese WESLEY
#> 3 Eli 12 2 mixed nuts ELI
#> 4 Toby 1 2 nerf bullets TOBY
```
Binary file modified ex05_nesting-is-good_files/figure-gfm/unnamed-chunk-7-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6bfd877

Please sign in to comment.