forked from matloff/TidyverseSkeptic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRDesign.tex
563 lines (345 loc) · 9.8 KB
/
RDesign.tex
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{times}
\usepackage{listings}
\usepackage{parcolumns}
\usepackage{hyperref}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.1in}
\title{R Design Patterns, Base-R vs.\ Tidyverse \\
With a view toward the teaching of R beginners}
\author{Norman Matloff \\
Dept. of Computer Science, University of California, Davis}
\begin{document}
\maketitle
This document enables the reader to see at a glance the difference
between base-R and the tidyverse in common R design settings. I believe
the base-R versions are generally simpler, thus more appropriate for R
learners. Relative to learning via base-R, learners who are taught Tidy
take longer to reach proficiency, and even then are proficient only in a
very narrow range of operations.
I discuss this in much more detail (e.g. an entire section on
pipes) in \url{http://github.com/matloff/TidyverseSkeptic/README.md},
where I cite many other problems besides lack of simplicity, but I
believe the examples here begin to illustrate why Tidy is a bad vehicle
to use in teaching R beginners. By the way, I use the latter term to
mean R learners with no prior programming background.
The examples here might also serve those who use base-R but wish to
learn Tidy, or vice versa.
All examples use R's built-in datasets. Note that, after changing a data
frame, it is restored for the next example, e.g. \textbf{data(mtcars)}.
The examples are presented roughly in order of how often these
operations tend to be performed by R users.
As this document is aimed at comparing base-R and the tidyverse in terms
of teaching new R learners, advanced functions from either base-R (e.g.
\textbf{with()} or the tidyverse (e.g. \textbf{mutate\_at()} are excluded
here. The same is true for advanced arguments of less-advanced
functions.
It is certainly not claimed here that all possible operations are
simpler in base-R, and indeed \textbf{ I've long called for teaching R
with a blend of the two approaches}, as seen in one of the examples
here.
\section*{Reading a specific cell in a data frame}
What is the MPG value for the third car?
\begin{parcolumns}[rulebetween=true]{2}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mtcars$mpg[3]
\end{lstlisting}
\end{minipage}
}
\hspace{0.1in}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mtcars %>%
select(mpg) %>%
slice(3)
\end{lstlisting}
\end{minipage}
}
\end{parcolumns}
\textbf{Comment:} The Tidy version could be shortened a bit by using
\lstinline{select(mtcars,mpg)} instead of
\lstinline{mtcars %>% select(mpg)},
but the latter seems to be the preferred form,
e.g. on the official tidyverse page, \textit{https://dplyr.tidyverse.org/}.
\section*{Adding a column to a data frame}
Create a new variable, the horsepower-to-weight ratio.
\begin{parcolumns}[rulebetween=true]{2}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mtcars$hwratio
<- mtcars$hp / mtcars$wt
\end{lstlisting}
\end{minipage}
}
\hspace{0.1in}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mtcars %>%
mutate(hwratio=hp/wt) -> mtcars
\end{lstlisting}
\end{minipage}
}
\end{parcolumns}
\bigskip
\textbf{Comment:}
Of course, typically Tidy coders would use \lstinline{<-}
rather than \lstinline{->}. I feel that the former is more
consistent with the ``left to right flow'' of pipes. But in any case,
the point about code complexity is the same either way.
\section*{Extracting rows}
Find all the cars with 8 cylinders.
\begin{parcolumns}[rulebetween=true]{2}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mtc8 <-
subset(mtcars,cyl==8)
\end{lstlisting}
\end{minipage}
}
\hspace{0.1in}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mtcars %>%
filter(cyl == 8) -> mtc8
\end{lstlisting}
\end{minipage}
}
\end{parcolumns}
\section*{Mean by group}
Find the mean MPG, broken down by number of cylinders.
\begin{parcolumns}[rulebetween=true]{2}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
tapply(mtcars$mpg,
mtcars$cyl,mean)
\end{lstlisting}
\end{minipage}
}
\hspace{0.1in}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mtcars %>%
group_by(cyl) %>%
summarize(meanMPG =
mean(mpg,))
\end{lstlisting}
\end{minipage}
}
\end{parcolumns}
\textbf{Comment:} Strangely, many Tidy advocates dismiss the
\textbf{tapply()} function, treating it as a niche function in base-R.
On the contrary, it is a workhorse in classic base-R applications. For
example, consider the \textbf{ggplot2} package, written by the (later)
inventor of the tidyverse, Hadley Wickham. Hadley calls
\textbf{tapply()} 7 times in the \textbf{ggplot2} code!
\section*{Row means}
For each day in the market, find the mean of 4 major EU stock indexes.
\begin{parcolumns}[rulebetween=true]{2}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
rowMeans(EuStockMarkets)
\end{lstlisting}
\end{minipage}
}
\hspace{0.1in}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
EuStockMarkets %>%
as.data.frame %>%
rowwise() %>%
mutate(m =
rowMeans(across(everything())))
%>% select(m)
\end{lstlisting}
\end{minipage}
}
\end{parcolumns}
\textbf{Comment:} The row means operation is quite common in R usage.
Here the Tidy user must go to much more trouble than in base-R.
\section*{Row operations, custom function}
For each market day, find the spread between the smallest index and the
largest.
\begin{parcolumns}[rulebetween=true]{2}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mM <- function(x)
max(x) - min(x)
apply(EuStockMarkets,1,mM)
\end{lstlisting}
\end{minipage}
}
\hspace{0.1in}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mM <- function(x)
max(x) - min(x)
EuStockMarkets %>%
as.data.frame %>% rowwise() %>%
mutate(m =
mM(across(everything()))) %>%
select(m)
\end{lstlisting}
\end{minipage}
}
\end{parcolumns}
\textbf{Comment:} The \textbf{apply()} function is of central importance
in traditional R. Here again, the Tidy user must go to much more
trouble.
\section*{Means, grouped by more than one variable}
Find the mean MPG, broken down by both number of cylinders and type of
transmission.
\begin{parcolumns}[rulebetween=true]{2}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
tapply(mtcars$mpg,
list(mtcars$cyl,
mtcars$am),
mean)
\end{lstlisting}
\end{minipage}
}
\hspace{0.1in}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mtcars %>%
group_by(cyl,am) %>%
summarize(m = mean(mpg))
\end{lstlisting}
\end{minipage}
}
\end{parcolumns}
\textbf{Comment:}
In this example, the two sets of code are not fully comparable,
as there is quite a difference in type of output:
\begin{lstlisting}
> tapply(mtcars$mpg,list(mtcars$cyl,mtcars$am),mean)
0 1
4 22.900 28.07500
6 19.125 20.56667
8 15.050 15.40000
> mtcars %>% group_by(cyl,am) %>% summarize(m = mean(mpg))
# Groups: cyl [3]
cyl am m
<dbl> <dbl> <dbl>
1 4 0 22.9
2 4 1 28.1
3 6 0 19.1
4 6 1 20.6
5 8 0 15.0
6 8 1 15.4
\end{lstlisting}
The base-R form returns a 3x2 table, which is often what one needs for
reports, research papers and so. The Tidy version is less useful in
such contexts, but would be of greater value in some other applications.
\section*{Binary recoding of a vector}
Record the river levels to 'high' if $>$ 1000, 'low' otherwise.
\begin{parcolumns}[rulebetween=true]{2}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
NileHiLow <-
ifelse(Nile >= 1000,
'high','low')
\end{lstlisting}
\end{minipage}
}
\hspace{0.1in}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
Nile %>% as.data.frame %>%
mutate(
HighLow = case_when
(x < 1000~'low',
x >= 1000~'high')
) %>%
select(HighLow) %>%
as.vector -> HighLow
\end{lstlisting}
\end{minipage}
}
\end{parcolumns}
\textbf{Comment:}
The Tidy step of conversion back to a vector at the end is needed for
the many R packages in which vector input is required.
\section*{Deleting columns from a data frame}
Remove the 'drat' and 'carb' variables from the data.
\begin{parcolumns}[rulebetween=true]{2}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mtcars[c('drat','carb')]
<- NULL
\end{lstlisting}
\end{minipage}
}
\hspace{0.1in}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
mtcars %>%
select(-c(drat,carb))
-> mtcars
\end{lstlisting}
\end{minipage}
}
\end{parcolumns}
\section*{Isn't It More Reasonable to Teach a Mix of Base-R and Tidy?}
I believe most people react skeptically to extreme ideologies. I've
advocated teaching a mix of base-R and Tidy.
Here is an example. (The Tidy version appeared in a presentation by a
Tidy advocate.) In the \textbf{mtcars} dataset, say we wish to recode
the number of gears into English.
\begin{parcolumns}[rulebetween=true]{2}
\colchunk{
\begin{minipage}{0.40\linewidth}
\begin{lstlisting}
gr <- mtcars$gear
mtcars$gear <-
case_when(
gr == 3 ~ 'three',
gr == 4 ~ 'four',
gr == 5 ~ 'five')
\end{lstlisting}
\end{minipage}
}
\hspace{0.05in}
\colchunk{
\begin{minipage}{0.45\linewidth}
\begin{lstlisting}
mtcars %>%
mutate(
gear =
case_when(
gear == 3 ~ "three",
gear == 4 ~ "four",
gear == 5 ~ "five"
)
) -> mtcars
\end{lstlisting}
\end{minipage}
}
\end{parcolumns}
\textbf{Comment:} A purely-base-R version might use nested
\textbf{ifelse()} calls. This would be shorter than the Tidy version,
but not as clear. But by using a blend of base-R and Tidy---use of '\$'
to reference a data frame column and use of the Tidy function
\textbf{case\_when()}---we still end up with shorter and simpler code,
compared to the pure-Tidy version.
\end{document}