-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathex02_create-or-mutate-in-place.R
81 lines (71 loc) · 2.1 KB
/
ex02_create-or-mutate-in-place.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#' ---
#' title: "Add or modify a variable"
#' author: "Jenny Bryan"
#' date: "`r format(Sys.Date())`"
#' output: github_document
#' ---
#+ setup, include = FALSE, cache = FALSE
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
error = TRUE
)
options(tidyverse.quiet = TRUE)
#+ body
# ----
library(tidyverse)
# ----
#' ### Function to produce a fresh example data frame
new_df <- function() {
tribble(
~ name, ~ age,
"Reed", 14L,
"Wesley", 12L,
"Eli", 12L,
"Toby", 1L
)
}
# ----
#' ## The `df$var <- ...` syntax
#' 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. This can be a drag.
#' * I have aesthetic concerns. YMMV.
df <- new_df()
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. 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, 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.
new_df() %>%
mutate(
eyes = 2L,
snack = c("chips", "cheese"),
uname = toupper(name)
)
#' Oops! I did not provide enough snacks!
new_df() %>%
mutate(
eyes = 2L,
snack = c("chips", "cheese", "mixed nuts", "nerf bullets"),
uname = toupper(name)
)