-
Notifications
You must be signed in to change notification settings - Fork 63
/
Getting-Started-in-R.Rmd
873 lines (658 loc) · 26.5 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
---
title: "Getting Started in R - Tidyverse Edition"
# Use letters for affiliations
author:
- name: Saghir Bashir
# 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](https://ilustat.com/) $\\bullet$ [[email protected]](mailto:[email protected])"
# Place DOI URL or CRAN Package URL here
doi: "Learn more at https://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 and the "tidyverse" package.
# Optional: Acknowledgements
# acknowledgements: |
# [Names] for reviewing the draft versions of this document.
# Optional: One or more keywords
keywords:
- R
- Tiyverse
- Statistics
- Data Science
# Paper size for document, values of letterpaper and a4paper
papersize: a4
# 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) #, knitr.table.format = 'latex')
library(tidyverse)
library(knitr)
library(kableExtra)
options(width=45)
knitr::opts_chunk$set(fig.pos = 'H')
# 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))
```
\begin{tikzpicture}[remember picture, overlay]
\node [shift={(-2cm,-2cm)}] at (current page.north east)
{\includegraphics[width=2.5cm]{images/tidyverse.png}};
\end{tikzpicture}
# Preface
This "Getting Started" guide will give you a flavour of what R[^baseR] and the
tidyverse can do for you. To get the most out of this guide, read it whilst
doing the examples and exercises using RStudio[^rstudio]^.
## 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://ilustat.com/shared/Getting-Started-in-R.zip](https://ilustat.com/shared/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("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
I 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")
```
## Help Examples
Use the `example()` function to run the examples at the end of the help for a function:
```{r egttest}
example(mean)
```
## 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(x, x1, x2, x3, xm, 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()` function) 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 eventually need to learn about them but for now you don't need to.
## R Packages
You can do many things with a standard R installation and it can be extended using
contributed packages. Packages are like apps for R. They can contain functions, data
and documentation.
## `tidyverse`
The `tidyverse` package[^tidyverse] is a collection of packages to import,
manipulate, explore, visualise and model data in a harmonised and consistent way which
helps you to be more productive. We will use the `tidyverse` to visualise and summarise
data.
## MUST DO: Ensure that the `tidyverse` package is installed
```{r installTidyverse, echo=TRUE, eval=FALSE}
install.packages("tidyverse")
```
## Loading packages
To use the `tidyverse` package load it using the `library()` function:
```{r usePackage}
library(tidyverse)
```
# Chick Weight Data
R comes with many datasets installed[^Rdatasets]. We will use the `ChickWeight` dataset
to learn about the tidyverse. 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}
ChickWeight %>%
select(Chick, Diet, Time, weight) %>%
arrange(Chick, Diet, Time) %>%
write_csv("ChickWeight.csv")
```
First we will import the data from a file called `ChickWeight.csv` using the `read_csv()`
function from the `readr` package (part of the `tidyverse`). 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 readCW}
CW <- read_csv("ChickWeight.csv")
```
All columns (variables) have been read in as numeric values (i.e. `col_double()`) but
you may see that they are read in a integer (i.e. `col_int()`) due to operating system
differences.
## 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].
# Looking at the Dataset
To look at the data type just type the object (dataset) name:
```{r printCW}
CW
```
## `glimpse()` function
If there are too many variables then not all them may be printed. To overcome this issue
we can use the `glimpse()` function which makes it possible to see every column in your
dataset (called a "data frame" in R speak).
```{r glimpseCW}
glimpse(CW)
```
## Interpretation
Both of these show 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 `ChicWeight.csv`.
So a good start.
## 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 (part of the
`tidyverse`). 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'}
# 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 (see the `<fct>` variables below) instead of continuous variables. It will
simplify our coding. The next section will explain the `mutate()` function.
```{r makeFactor}
CW <- mutate(CW, Diet = factor(Diet))
CW <- mutate(CW, Time = factor(Time))
glimpse(CW)
```
## `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.y="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.y="mean", geom="line",
colour="black") +
theme(legend.position = "none") +
ggtitle("Chick Weight over Time by Diet") +
xlab("Time (days)") +
ylab("Weight (grams)")
```
# Tidyverse: Data Wrangling Basics
```{r resettblprint, echo = FALSE}
# Temporarily resetting the print limit
options(tibble.print_min=5, tibble.print_max=5)
```
In this section we will learn how to wrangle (manipulate) datasets using the `tidyverse`
package. Let's start with the `mutate()`, `select()`, `rename()`, `filter()` and `arrange()`
functions.
## `mutate()`
Adds a new variable (column) or modifies an existing one. We already used this above to create
factor variables.
```{r mutateDemo}
# Added a column
CWm1 <- mutate(CW, weightKg = weight/1000)
CWm1
# Modify an existing column
CWm2 <- mutate(CW, Diet = str_c("Diet ", Diet))
CWm2
```
## `select()`
Keeps, drops or reorders variables.
```{r selectDemo}
# Drop the weight variable from CWm1 using minus
select(CWm1, -weight)
# Keep variables Time, Diet and weightKg
select(CWm1, Chick, Time, Diet, weightKg)
```
## `rename()`
Renames variables whilst keeping all variables.
```{r renameDemo}
rename(CW, Group = Diet, Weight = weight)
```
## `filter()`
Keeps or drops observations (rows).
```{r filterDemo}
filter(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).
## `arrange()`
Changes the order of the observations (rows).
```{r arrangeDemo}
arrange(CW, Chick, Time)
arrange(CW, desc(weight))
```
## Exercise
What does the `desc()` do? Try using `desc(Time)`.
# Tidyverse: Pipe operator ` %>%`
In reality you will end up doing multiple data wrangling steps that you will want
to save. This is where the pipe operator, `%>%`, plays a very useful role. Let's
have a look at an example:
```{r pipeOpDemo}
CW21 <- CW %>%
filter(Time %in% c(0, 21)) %>%
rename(Weight = weight) %>%
mutate(Group = factor(str_c("Diet ", Diet))) %>%
select(Chick, Group, Time, Weight) %>%
arrange(Chick, Time)
CW21
```
## Read `%>%` as "then"
To understand the code above we should read the pipe operator ` %>%` as "then".
> Create a new dataset (object) called `CW21` using dataset `CW` ***then***
keep the data for days 0 and 21 ***then*** rename variable `weight` to `Weight`
***then*** create a variable called `Group` ***then*** keep variables `Chick`,
`Group`, `Time` and `Weight` and ***then*** finally arrange the data by
variables `Chick` and `Time`.
## In practice
```{r pipeOpExplain, echo=TRUE, eval=FALSE}
CW21 <- CW %>%
filter(., Time %in% c(0, 21)) %>%
rename(., Weight = weight) %>%
mutate(., Group=factor(str_c("Diet ",Diet))) %>%
select(., Chick, Group, Time, Weight) %>%
arrange(., Chick, Time)
```
The pipe operator, `%>%`, replaces the dots (`.`) with whatever is returned from code
preceding it. For example, the dot in `filter(., Time %in% c(0, 21))` is replaced by
`CW`. The output of the `filter(...)` then replaces the dot in
`rename(., Weight = weight)` and so on. Think of it as a data assembly line with
each function doing its thing and passing it to the next.
# 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 by **diet** and **time**.
```{r mnsdStatCW}
mnsdCW <- CW %>%
group_by(Diet, Time) %>%
summarise(N = n(), Mean = mean(weight)) %>%
arrange(Diet, Time)
mnsdCW
```
## `group_by()` function
For each distinct combination of `Diet` and `Time`, the chick weight data is summarised
into the number of observations (`N`) and the mean (`Mean`) of `weight`.
## Other summaries
Let's also calculate the standard deviation, median, minimum and maximum values but only
at days 0 and 21.
```{r sumCW}
sumCW <- CW %>%
filter(Time %in% c(0, 21)) %>%
group_by(Diet, Time) %>%
summarise(N = n(),
Mean = mean(weight),
SD = sd(weight),
Median = median(weight),
Min = min(weight),
Max = max(weight)) %>%
arrange(Diet, Time)
sumCW
```
Let's make the summaries "prettier", say, for a report or publication.
```{r prettySum}
prettySumCW <- sumCW %>%
mutate(Mean_SD = str_c(format(Mean, digits=1),
" (", format(SD, digits=2), ")")) %>%
mutate(Range = str_c(Min, " - ", Max)) %>%
select(Diet, Time, N, Mean_SD, Median, Range) %>%
arrange(Diet, Time)
prettySumCW
```
## Final Table
Eventually you should be able to produce[^kable] a publication ready version as follows:
```{r prettyKable, echo = FALSE}
prettySumCW %>%
kable("latex", booktabs = TRUE, linesep = "", align = "cccrc") %>%
kable_styling(position = "center") %>%
row_spec(0, bold = TRUE) %>%
row_spec(c(2, 4, 6, 8), background = "lightgray")
# Uncomment below once bugs are fixed - https://github.com/haozhu233/kableExtra/issues/243 & 237
# column_spec(1, bold = T) %>%
# collapse_rows(columns = 1, latex_hline = "none", valign = "top")
```
## 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.
```{r tblprintdef, echo = FALSE}
# Resetting the print limit back to the defaults
options(tibble.print_min=10, tibble.print_max=20)
```
[^baseR]: R project: [https://www.r-project.org/](https://www.r-project.org/)
[^tidyverse]: Tidyverse: [https://www.tidyverse.org/](https://www.tidyverse.org/)
[^rstudio]: RStudio IDE: [https://www.rstudio.com/products/RStudio/](https://www.rstudio.com/products/RStudio/)
[^setwd]: Use `getwd()` to see the current working directory and `setwd("/to/data/path/data.csv")` to change it (important to use `/` even for Microsoft Windows).
[^Rdatasets]: Type `data()` in the R console to see a list of the datasets.
[^kable]: Using the `kable()` function from the `knitr` package with functions from the `kableExtra` package.
[^CWfup]: I 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."*