-
Notifications
You must be signed in to change notification settings - Fork 27
/
Getting-Started-in-R.Rmd
932 lines (696 loc) · 29.4 KB
/
Getting-Started-in-R.Rmd
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
---
title: "Getting Started in R: Tinyverse Edition"
# Use letters for affiliations
author:
- name: Saghir Bashir and Dirk Eddelbuettel
# affiliation: a
# address:
# - code: a
# address: ilustat \url{www.ilustat.com}
# For footer text TODO(fold into template, allow free form two-authors)
# I will use it to place the copyright notice (Creative Comments Share Alike)
lead_author_surname: "[CC BY SA](https://creativecommons.org/licenses/by-sa/4.0/) [ilustat](http://ilustat.com/) $\\bullet$ [[email protected]](mailto:[email protected]) $\\bullet$ [[email protected]](mailto:[email protected])"
# Place DOI URL or CRAN Package URL here
doi: "Learn more at http://ilustat.com/resources/"
# Abstract
abstract: |
Are you curious to learn what R can do for you? Do you want to see how it works?
Yes, then this "Getting Started" guide is for you. It uses realistic examples and a
real life dataset to manipulate, visualise and summarise data. By the end of it
you will have an overview of the key concepts of R.
# Optional: Acknowledgements
# acknowledgements: |
# [Names] for reviewing the draft versions of this document.
# Optional: One or more keywords
keywords:
- R
- Statistics
- Data Science
- Tinyverse
# Paper size for document, values of letterpaper and a4paper
#papersize: a4
papersize: letter
# Font size of the document, values of 9pt (default), 10pt, 11pt and 12pt
fontsize: 9pt
# Optional: Force one-column layout, default is two-column
# one_column: true
# Optional: Enables lineno mode, but only if one_column mode is also true
#lineno: true
# Optional: Enable one-sided layout, default is two-sided
#one_sided: true
# Optional: Enable section numbering, default is unnumbered
numbersections: true
# Optional: Specify the depth of section number, default is 5
secnumdepth: 3
# Optional: Skip inserting final break between acknowledgements, default is false
skip_final_break: true
# Optional: Bibliography
# bibliography: pinp
# Optional: Enable a 'Draft' watermark on the document
watermark: FALSE
# Customize footer, eg by referencing the vignette
footer_contents: "Getting Started in R"
# Produce a pinp document
output:
pinp::pinp:
fig_caption: yes
collapse: true
keep_tex: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
eval = TRUE,
cache = FALSE,
fig.pos = 'H') #, knitr.table.format = 'latex')
library(data.table)
library(ggplot2)
library(knitr)
library(tinytable)
options(width=50)
# Making some aesthetic changes for this document
theme_set(theme_gray(base_size = 9))
update_geom_defaults("point", list(size = 0.5))
update_geom_defaults("boxplot", list(outlier.size = 0.5))
# Temporarily resetting the print limit
op <- options()
options(datatable.print.topn=3, width=50, datatable.print.class=FALSE)
```
# Preface
This "Getting Started" guide will give you a flavour of what R[^baseR]
can do for you. To get the most out of this guide, read it whilst
doing the examples and exercises using RStudio[^rstudio]^.
This note is a variant of the original document[^gettingstarted] but stresses the use of Base R
along with careful dependency management as discussed below.
## Experiment Safely
Be brave and experiment with commands and options as it is an essential part of the
learning process. Things can (and will) go "wrong", like, getting error messages or
deleting things that you create by using this guide. You can recover from most
situations (e.g. by restarting R). To do this "safely" start with a *fresh* R session
without any other data loaded (otherwise you could lose it).
# Introduction
## Before Starting
Make sure that:
1. R and RStudio are installed.
2. [https://eddelbuettel.github.io/gsir-te/Getting-Started-in-R.zip](https://eddelbuettel.github.io/gsir-te/Getting-Started-in-R.zip) has
been downloaded and unzipped
3. Double click `"Getting-Started-in-R.Rproj"` to open RStudio with the setup for this guide.
## Starting R & RStudio
R starts automatically when you open RStudio (see Figure \ref{fig:rstudio}). The console
starts with information about the version number, license and contributors.
The last line is a standard prompt "`>`" that indicates R is ready and expecting
instructions to do something.
```{r RStudioScreenshot, out.width="3.4in", fig.show='hold', fig.cap="\\label{fig:rstudio}RStudio Screenshot with Console on the left and Help tab in the bottom right", echo=FALSE}
include_graphics("figures/RStudio-Screenshot.png")
```
## Quitting R & RStudio
When you quit RStudio you will be asked whether to `Save workspace` with two options:
* "Yes" -- Your current R workspace (containing the work that you have done) will be
restored next time you open RStudio.
* "No" -- You will start with a fresh R session next time you open RStudio. For now
select "*No*" to prevent errors being carried over from previous sessions).
# R Help
We strongly recommend that you learn how to use R's useful and extensive built-in help
system which is an essential part of finding solutions to your R programming problems.
## `help()` function
From the R "Console" you can use the `help()` function or `?`. For example, try the following two
commands (which give the same result):
```{r helpttest, echo=TRUE, eval=FALSE}
help(mean)
?mean
```
## Keyword search
To do a keyword search use the function `apropos()` with the keyword in double quotes
(`"keyword"`) or single quote (`'keyword'`). For example:
```{r aproposShow, echo=TRUE, eval=TRUE}
apropos("mean") |> head(16)
```
## Help Examples
Use the `example()` function to run the examples at the end of the help for a function:
```{r egttest, echo=2}
options(prompt="> ")
example(mean)
options(prompt="R> ")
```
## RStudio Help
Rstudio provides search box in the "Help" tab to make your life easier (see Figure
\ref{fig:rstudio}).
## Searching On-line For R Help
There are a lot of on-line resources that can help. However you must understand that
blindly copying and pasting could be harmful and further it won't help you to learn and
develop. When you search on-line use `[R]` in your search term (e.g. "[R] summary
statistics by group"). Note that often there is more than one solution to your problem.
It is good to investigate the different options.
## Exercise
Try the following:
1. `help(median)`
2. `?sd`
3. `?max`
## Warning
If an R command is not complete then R will show a plus sign (`+`) prompt on second and subsequent
lines until the command syntax is correct.
```{r continuation, echo=TRUE, eval=FALSE}
+
```
To break out this, press the escape key (`ESC`).
## Hint
To recall a previously typed commands use the up arrow key ($\uparrow$). To go between previously
typed commands use the up and down arrow ($\downarrow$) keys. To modify or correct a command use the
left ($\leftarrow$) and right arrow ($\rightarrow$) keys.
# Some R Concepts
In R speak, scalars, vectors/variables and datasets are called ***objects***. To create
objects (things) we have to use the assignment operator `<-`. For example, below, object
`height` is assigned a value of 173 (typing `height` shows its value):
```{r createObjs}
height <- 173
height
```
## Warning: R is case sensitive
`age` and `AgE` are different:
```{r caseSens1}
age <- 10
AgE <- 50
```
```{r caseSens2}
age
AgE
```
## New lines
R commands are usually separated by a new line but they can also be separated by a
semicolon: `;`.
```{r semicolon}
Name <- "Leo"; Age <- 25; City <- "Lisbon"
Name; Age; City
```
## Comments
It is useful to put human readable comments in your programs. These comments could
help the future you when you go back to your program. R comments start with a hash
sign (`#`). Everything after the hash to the end of the line will be ignored by R.
```{r comments}
# This comment line will be ignored when run.
City # Text after "#" is ignored.
```
# R as a Calculator
You can use R as a calculator. Try the following:
```{r calcBasic}
2 + 3
(5*11)/4 - 7
# ^ = "to the power of"
7^3
```
## Other math functions
You can also use standard mathematical functions that are typically found on a scientific
calculator.
* Trigonometric: `sin()`, `cos()`, `tan()`, `acos()`, `asin()`, `atan()`
* Rounding: `abs()`, `ceiling()`, `floor()`, `round()`, `sign()`, `signif()`, `sqrt()`,
`trunc()`
* Logarithms & Exponentials: `exp()`, `log()`, `log10()`, `log2()`
```{r calcFunctions}
# Square root
sqrt(2)
# Round down to nearest integer
floor(8.6178)
# Round to 2 decimal places
round(8.6178, 2)
```
## Exercise
What do the following pairs of examples do?
1. `ceiling(18.33)` and `signif(9488, 2)`
2. `exp(1)` and `log10(1000)`
3. `sign(-2.9)` and `sign(32)`
4. `abs(-27.9)` and `abs(11.9)`
# Some More R Concepts
You can do some clever and useful things with using the assignment operator "`<-`":
```{r assignBasic}
roomLength <- 7.8
roomWidth <- 6.4
roomArea <- roomLength * roomWidth
roomArea
```
## Text objects
You can also assign text to an object.
```{r assignText}
Greeting <- "Hello World!"
Greeting
```
## Vectors
The objects presented so far have all been scalars (single values). Working with vectors
is where R shines best as they are the basic building blocks of datasets. To create a
vector we can use the `c()` (combine values into a vector) function.
```{r cVector}
# A "numeric" vector
x1 <- c(26, 10, 4, 7, 41, 19)
x1
# A "character" vector of country names
x2 <- c("Peru", "Italy", "Cuba", "Ghana")
x2
```
There are many other ways to create vectors, for example, `rep()` (replicate elements)
and `seq()` (create sequences):
```{r repseq}
# Repeat vector (2, 6, 7, 4) three times
r1 <- rep(c(2, 6, 7, 4), times=3)
r1
# Vector from -2 to 3 incremented by half
s1 <- seq(from=-2, to=3, by=0.5)
s1
```
## Vector operations
You can also do calculations on vectors, for example using `x1` from above:
```{r operationsVecs}
x1 * 2
round(sqrt(x1*2.6), 2)
```
## Missing Values
Missing values are coded as `NA` in R. For example,
```{r MissingValues}
x2 <- c(3, -7, NA, 5, 1, 1)
x2
x3 <- c("Rat", NA, "Mouse", "Hamster")
x3
```
## Managing Objects
Use function `ls()` to list the objects in your workspace. The `rm()` function removes
(deletes) them.
```{r lsrm}
ls()
rm(x1, x2, x3, r1, s1, AgE, age)
ls()
```
## Exercise
Calculate the `gross` by adding the `tax` to `net` amount.
```
net <- c(108.99, 291.42, 16.28, 62.29, 31.77)
tax <- c(22.89, 17.49, 0.98, 13.08, 6.67)
```
# R Functions and Packages
## R Functions
We have already used some R functions (e.g. `c()`, `mean()`, `rep()`, `sqrt()`, `round()`).
Most of the computations in R involves using functions. A function essentially has
a name and a list of arguments separated by a comma. Let's have look at an example:
```{r functionUsage}
seq(from = 5, to = 8, by = 0.4)
```
The function name is `seq` and it has three arguments `from`, `to` and `by`. The arguments
`from` and `to` are the start and end values of a sequence that you want to create,
and `by` is the increment of the sequence. The `seq()` functions has other arguments that
you could use which are documented in the help page. For example, we could use the argument
`length.out` (instead of `by`) to fix the length of the sequence as follows:
```{r functionAlt}
seq(from = 5, to = 8, length.out = 16)
```
## Custom Functions
You can create your own functions (using the `function()` keyword) which is a
very powerful way to extend R. Writing your own functions is outside the scope
of this guide. As you get more and more familiar with R it is very likely that you
will need to learn how to do so but for now you don't need to.
## R Packages
You can already do many things with a standard R installation---but it can be extended
using contributed packages. Packages are like apps for R. They can contain functions, data
and documentation.
## Extending Base R
Base R already comes with over two-thousand functions that have been proven to be
versatile, reliable and stable. That is no small feat. When it is possible to solve a
problem with _fewer_ external dependencies, doing so follows time-honoured best practices.
You want to think carefully before adding dependencies.
## The `tinyverse` View
The philosophy of _less is more_ is at the core of the tinyverse[^tinyverse]. Fewer
dependencies means a smaller footprint, faster installation, and most importantly fewer
nodes in your dependency graph. Experience, as well as empirical and theoretical
software engineering practice have demonstrated that failure increases with complexity.
So choosing when to rely on additional packages has to balance the increased functionality
a package brings with both its history of development, its development model, maintenance
status, and history of both changes and fixes. This _is_ a complex topic, and there are no
easy answers. But by adding another package, we always open a door to interface changes
we no longer control. The added functionality is clearly valuable at times, yet one has
to remain aware of the costs that may accrue as a consequence. So this document takes the
view that _fewer is better_, and will rely on only two additional packages:
`data.table`[^datatable] for data wrangling as well as input/output, and `ggplot2`
[^ggplot2] for visualization.
## Installation
If needed, install these two packages via the following command which should pick
the suitable version for your installation:
```{r installation, eval=FALSE}
install.packages(c("data.table", "ggplot2"))
```
# Chick Weight Data
R comes with many datasets installed[^Rdatasets]. We will use the `ChickWeight` dataset
to learn about data manipulation. The help system gives a basic summary of the experiment from
which the data was collect:
> *"The body weights of the chicks were measured at birth and every second day thereafter
until day 20. They were also measured on day 21. There were four groups of chicks on
different protein diets."*
You can get more information, including references by typing:
```{r helpCWdata, echo=TRUE, eval=FALSE}
help("ChickWeight")
```
## The Data
There are `r nrow(ChickWeight)` observations (rows) and `r ncol(ChickWeight)` variables:
* `Chick` -- unique ID for each chick.
* `Diet` -- one of four protein diets.
* `Time` -- number of days since birth.
* `weight` -- body weight of chick in grams.
## Note
`weight` has a lower case `w` (recall R is case sensitive).
## Objective
Investigate the *effect of diet on the weight over time*.
# Importing The Data
```{r writeCW, echo=FALSE, message=FALSE, warning=FALSE, eval=TRUE}
## data.table already loaded above
CW <- data.table(ChickWeight)
if (!file.exists("ChickWeight.csv"))
fwrite(CW[, .(Chick, Diet, Time, weight)],
file="ChickWeight.csv")
```
First we will import the data from a file called `ChickWeight.csv` using the
`fread()` function from the `data.table` package which returns a `data.table`
object (whereas the dataset built into R has a different format). The first
thing to do, outside of R, is to open the file `ChickWeight.csv` to check
what it contains and that it makes sense. Now we can import the data as
follows:
```{r cwdt}
suppressMessages(library(data.table)) # tinyverse
cw <- fread("ChickWeight.csv")
```
## Important Note
If all goes well then the data is now stored in an R object called `cw`. If you get the
following error message then you need to change the working directory to where the data is
stored.
```
Error: 'ChickWeight.csv' does not exist in current
working directory ...
```
## Change the working directory in RStudio
From the menu bar select "Session - Set Working Directory - Choose Directory..." then
go to the directory where the data is stored. Alternatively, within in R, you could use
the function `setwd()`[^setwd]. You can also specify a full path, using `~` to denote your home
directory.
# Looking at the Dataset
To look at the data type just type the object (dataset) name:
```{r printcw}
cw
```
Several base R functions help us inspect the data: `str()` compactly displays the structure,
`summary()` provides a summary, and `head()` and `tail()` display the beginning and end of the data
set.
```{r cwsummaryActual, echo=3:4, eval=TRUE, result="asis"}
str <- function(object, ...) {
if (!is.data.frame(object)) {
warning("str.data.frame() called with non-data.frame -- coercing to one.")
object <- data.frame(object)
}
cl <- oldClass(object)
cl <- cl[cl != "data.frame"]
if (0 < length(cl))
cat("Classes", paste(sQuote(cl), collapse = ", "), "and ")
cat("'data.frame':\n ", nrow(object), " obs. of ", (p <- length(object)),
" variable", if (p != 1) "s", if (p > 0) ":", "\n", sep = "")
#if (length(l <- list(...)) && any("give.length" == names(l)))
# invisible(NextMethod("str", ...))
#else invisible(NextMethod("str", give.length = FALSE, ...))
utils:::str.default(object, vec.len=2, strict.width="wrap", ...)
}
options(digits=5,width=45)
str(cw)
summary(cw)
options(digits=6,width=50)
```
## Interpretation
This shows that the dataset has `r nrow(cw)` observations and `r ncol(cw)` variables as we would
expect, and as compared to the original data file `ChickWeight.csv`. So a good start. `str()` call
notes the types of variables (all `integer` here) and the first few values. The RStudio
'Environment' pane provides a very similar view.
## Exercise
It is important to look at the last observations of the dataset as it could reveal
potential data issues. Use the `tail()` function to do this. Is it consistent with the
original data file `ChickWeight.csv`?
# Chick Weight: Data Visualisation
## `ggplot2` Package
To visualise the chick weight data, we will use the `ggplot2` package. Our interest is in seeing how
the *weight changes over time for the chicks by diet*. For the moment don't worry too much about the
details just try to build your own understanding and logic. To learn more try different things even
if you get an error messages.
## First plot
Let's plot the weight data (vertical axis) over time (horizontal axis).
```{r emptyPlot, fig.width=1.74, fig.height=1.74, fig.show='hold', fig.align='center'}
# (Silently) load the plotting package
suppressMessages(library(ggplot2))
# An empty plot (the plot on the left)
ggplot(cw, aes(Time, weight))
# With data (the plot on the right)
ggplot(cw, aes(Time, weight)) + geom_point()
```
## Exercise
Switch the variables `Time` and `weight` in code used for the plot on the right?
What do you think of this new plot compared to the original?
## Add colour for `Diet`
The graph above does not differentiate between the diets. Let's use a different colour for
each diet.
```{r addColourPlot, fig.height=2.0}
# Adding colour for diet
ggplot(cw, aes(Time,weight,colour=factor(Diet))) +
geom_point()
```
## Interpretation
It is difficult to conclude anything from this graph as the points are printed on top of
one another (with diet 1 underneath and diet 4 at the top).
## Factor Variables
Before we continue, we have to make an important change to the `cw` dataset by making
`Diet` and `Time` *factor variables*. This means that R will treat them as categorical
variables instead of continuous variables. It will simplify our coding.
```{r cwfactor, echo=2:4, result="asis"}
options(digits=5,width=45)
cw[, Diet := factor(Diet)]
cw[, Time := factor(Time)]
str(cw) # notice the difference ?
options(digits=6,width=50)
```
Notice that the `:=` operator altered the variable "in-place", and no explicit assignment was
made. This is a key feature of `data.table` which operated "by reference": changes are made in
reference to one instance of the `cw` variable, rather than by creating updated copies. We will
revisit this `:=` assignment below.
## `facet_wrap()` function
To plot each diet separately in a grid using `facet_wrap()`:
```{r jitterPlot}
# Adding jitter to the points
ggplot(cw, aes(Time, weight, colour=Diet)) +
geom_point() +
facet_wrap(~ Diet) +
theme(legend.position = "bottom")
```
## Exercise
To overcome the issue of overlapping points we can ***jitter*** the points using
`geom_jitter()`. Replace the `geom_point()` above with `geom_jitter()`. What do you
observe?
## Interpretation
Diet 4 has the least variability but we can't really say anything about the mean effect
of each diet although diet 3 seems to have the highest.
## Exercise
For the `legend.position` try using "top", "left" and "none". Do we really need a legend
for this plot?
## Mean line plot
Next we will plot the mean changes over time for each diet using the `stat_summary()`
function:
```{r meanlinesPlot, fig.height=2.0}
ggplot(cw, aes(Time, weight,
group=Diet, colour=Diet)) +
stat_summary(fun="mean", geom="line")
```
## Interpretation
We can see that diet 3 has the highest mean weight gain by the end of the experiment but
we don't have any information about the variation (uncertainty) in the data.
## Exercise
What happens when you add `geom_point()` to the plot above? Don't forget the `+`. Does it
make a difference if you put it before or after the `stat_summary(...)` line? Hint: Look
very carefully at how the graph is plotted.
## Box-whisker plot
To see variation between the different diets we use `geom_boxplot` to plot a box-whisker
plot. A note of caution is that the number of chicks per diet is relatively low to
produce this plot.
```{r boxPlot}
ggplot(cw, aes(Time, weight, colour=Diet)) +
facet_wrap(~ Diet) +
geom_boxplot() +
theme(legend.position = "none") +
ggtitle("Chick Weight over Time by Diet")
```
## Interpretation
Diet 3 seems to have the highest "average" weight gain but it has more variation
than diet 4 which is consistent with our findings so far.
## Exercise
Add the following information to the above plot:
* x-axis label (use `xlab()`): "Time (days)"
* y-axis label (use `ylab()`): "Weight (grams)"
## Final Plot
Let's finish with a plot that you might include in a publication.
```{r finalPlot}
ggplot(cw, aes(Time, weight, group=Diet,
colour=Diet)) +
facet_wrap(~ Diet) +
geom_jitter() +
stat_summary(fun="mean", geom="line",
colour="black") +
theme(legend.position = "none") +
ggtitle("Chick Weight over Time by Diet") +
xlab("Time (days)") +
ylab("Weight (grams)")
```
# `data.table` Data Wrangling Basics
In this section we will learn how to wrangle (manipulate) datasets using the `data.table`
package. Conceptually, `data.table` operations can be viewed as `dt[i, j, by]` with some
intentional similarity to SQL. Here `i` can select (or subset) rows, `j` is used to
select, summarise or mutate columns, and `by` is the grouping operator. Numerous examples
follow.
## `j` to select (or transform) columns
Adds a new variable (column) or modifies an existing one. We already used this above to
create factor variables.
```{r dtmutate}
cw[, weightKg := weight/1000] # add a column
cw
cw[, Diet := paste0("Diet_", Diet)] # mod col.
cw
```
## `j` to select (or transform) columns
Keeps, drops or reorders variables.
```{r dtselect}
# Keep variables Time, Diet and weightKg
cw[, .(Chick, Time, Diet, weightKg)]
```
## `j` to summarise
It can be used to create aggregations, which is particularly handy with the grouping
operator. The following example computes means and standard deviations of the 'weight'
variable grouped by 'Diet'. Note that the output has been truncated.
```{r condmean, echo=3}
dt.print.nrows=getOption("datatable.print.nrows")
options(datatable.print.nrows=8)
cw[, .(Mean=mean(weight),SDev=sd(weight)),
by=.(Diet, Time)]
options(datatable.print.nrows=dt.print.nrows)
```
## `setnames()` to name or rename
Renames variables whilst keeping all variables.
```{r dtrename}
setnames(cw, c("Diet", "weight"),
c("Group", "Weight"))
cw
```
## `i` operator
Keeps or drops observations (rows).
```{r dtfilter}
cw[Time == 21 & Weight > 300]
```
For comparing values in vectors use: `<` (less than), `>` (greater than), `<=`
(less than and equal to), `>=` (greater than and equal to), `==` (equal to) and `!=`
(not equal to). These can be combined logically using `&` (and) and `|` (or).
## Keying observations
Setting a key changes the order of the observations (rows), and also makes indexing faster.
```{r dtarrange}
cw[order(Weight)] # on the fly
setkey(cw, Chick, Time) # setting a key
cw
```
## Exercise
What does the `order()` do? Try using `order(Time)` and `order(-Time)` in the `i` column.
# Chaining
You may want to do multiple data wrangling steps at once. This is where the 'chaining' of
`data.table` operations (_i.e._, several sets of commands with square brackets) comes to
the rescue:
```{r cwchained}
cw21 <- cw[Time %in% c(0,21)][ # i: select rows
, weight := Weight][ # j: mutate
, Group := factor(Group)][
, .(Chick,Group,Time,weight)][ # j: arrange
order(Chick,Time)][ # i: order
1:5] # i: subset
```
# Chick Weight: Summary Statistics
From the data visualisations above we concluded that the diet 3 has the highest mean
and diet 4 the least variation. In this section, we will quantify the effects of the
diets using summary statistics. We start by looking at the number of observations
and the mean of weight grouped by **diet** and **time**.
```{r ctstats}
cw[, .(N = .N, # .N is nb per group
Mean = mean(Weight)), # compute mean
by=.(Group, Time)][ # group by Diet + Time
1:5] # display rows 1 to 5
```
## `by=` argument
For each distinct combination of `Diet` and `Time`, the chick weight data is summarised
into the number of observations (`N`, using the internal variable `.N` denoting current
group size) and the mean (`Mean`) of `weight`.
## Other summaries
We can calculate the standard deviation, median, minimum and maximum values---only at days
0 and 21.
```{r dtSum, echo=2:3}
options(digits=3) # tighter display here
cws <- cw[Time %in% c(0,21),
.(N = .N,
Mean = mean(Weight),
SDev = sd(Weight),
Median = median(Weight),
Min = min(Weight),
Max = max(Weight) ),
by=.(Group, Time)]
cws
```
Finally, we can make the summaries "prettier" for a possible report or publication where we
format the numeric values as text.
```{r dtpretty}
cws[, Mean_SD := paste0(format(Mean,digits=1),
" (",
format(SDev,digits=2),
")")]
cws[, Range := paste(Min, "-", Max)]
prettySum <- cws[ , .(Group, Time, N, Mean_SD,
Median, Range)][
order(Group, Time)]
prettySum
```
## Final Table
Eventually you should be able to produce a publication-ready version such as the
following table, courtesy of the `tinytable` package.
```{r dtprettytable}
# library(tinytable)
prettySum |>
tt(theme = "striped") |>
style_tt(i = 0, bold = TRUE) |>
format_tt(escape = TRUE)
```
## Interpretation
This summary table offers the same interpretation as before, namely that diet 3 has the
highest mean and median weights at day 21 but a higher variation than group 4.
However it should be noted that at day 21, diet 1 lost 4 chicks from 20 that started
and diet 4 lost 1 from 10. This could be a sign of some issues (e.g. safety).
## Limitations of data
Information on bias reduction measures is not given and is not available either[^CWfup].
We don't know if the chicks were fairly and appropriately randomised to the diets and
whether the groups are comparable (e.g., same breed of chicks, sex (gender) balance).
Hence we should be very cautious with drawing conclusion and taking actions with this
data.
# Conclusion
This "Getting Started in R" guide introduced you to some of the basic concepts underlying
R and used a real life dataset to produce some graphs and summary statistics. It is only
a flavour of what R can do but hopefully you have seen some of power of R and its
potential.
## What next
There are plenty of R courses, books and on-line resources that you can learn from. It
is hard to recommend any in particular as it depends on how you learn best. Find things
that work for you (paying attention to the quality) and don't be afraid to make mistakes
or ask questions. Most importantly have fun.
# Acknowledgements
Special thanks to [Saghir Bashir](https://github.com/saghirb) for publishing the initial version of
[_Getting Started with R_](https://github.com/saghirb/Getting-Started-in-R), [Brodie
Gaslam](https://github.com/brodieG) and [Matt Dowle](https://github.com/mattdowle) for feedback on
this version, and to [Joshua Ulrich](https://github.com/joshuaulrich/) for many discussions about
[The Tinyverse](http://tinyverse.org).
[^baseR]: R project: [https://www.r-project.org/](https://www.r-project.org/)
[^tinyverse]: Tinyverse: [http://www.tinyverse.org/](http://www.tinyverse.org/)
[^rstudio]: RStudio IDE: [https://www.rstudio.com/products/RStudio/](https://www.rstudio.com/products/RStudio/)
[^setwd]: Use `getwd()` for the current directory and `setwd("/to/data/path/data.csv")` to change it.
[^Rdatasets]: Type `data()` in the R console to see a list of the datasets.
[^CWfup]: I (ie Saghir) contacted the source authors and kindly received the following reply *"They were mainly undergraduate projects, final-year, rather than theses, so, unfortunately, it's unlikely that any record remains, particularly after so many years."*
[^datatable]: data.table: [http://r-datatable.com](http://r-datatable.com)
[^ggplot2]: ggplot2: [https://ggplot2.tidyverse.org](https://ggplot2.tidyverse.org)
[^gettingstarted]: Getting Started with R: [https://github.com/saghirb/Getting-Started-in-R](https://github.com/saghirb/Getting-Started-in-R)
```{r reset, include=FALSE}
options(op)
```