-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathex01_leave-it-in-the-data-frame.R
59 lines (51 loc) · 1.52 KB
/
ex01_leave-it-in-the-data-frame.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
#' ---
#' title: "Leave your data in that big, beautiful data frame"
#' 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
# ----
#' ## Don't create odd little excerpts and copies of your data.
#'
#' Code style that results from (I speculate) minimizing the number of key
#' presses.
## :(
sl <- iris[51:100,1]
pw <- iris[51:100,4]
plot(sl ~ pw)
#' This clutters the workspace with "loose parts", `sl` and `pw`. Very soon, you
#' are likely to forget what they are, which `Species` of `iris` they represent,
#' and what the relationship between them is.
# ----
#' ## Leave the data *in situ* and reveal intent in your code
#'
#' More verbose code conveys intent. Eliminating the Magic Numbers makes the
#' code less likely to be, or become, wrong.
#'
#' Here's one way to do same in a tidyverse style:
library(tidyverse)
ggplot(
filter(iris, Species == "versicolor"),
aes(x = Petal.Width, y = Sepal.Length)
) + geom_point()
#' Another tidyverse approach, this time using the pipe operator, `%>%`
iris %>%
filter(Species == "versicolor") %>%
ggplot(aes(x = Petal.Width, y = Sepal.Length)) + ## <--- NOTE the `+` sign!!
geom_point()
#' A base solution that still follows the principles of
#'
#' * leave the data in data frame
#' * convey intent
plot(
Sepal.Length ~ Petal.Width,
data = subset(iris, subset = Species == "versicolor")
)