forked from anjiecao/tidyverse-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidyverse_examples.Rmd
90 lines (50 loc) · 1.97 KB
/
tidyverse_examples.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
---
title: "Tidyverse Examples"
author: "Psych 251 Staff"
date: "10/2/2019"
output: html_document
---
```{r setup, include=FALSE}
library(tidyverse)
```
# Manipulating data with dplyr
Let's use `mtcars`, a built in dataset of cars and their miles/gallon (mpg), number of cylinders (cyl), displacement (disp), gross horsepower (hp), etc.
```{r}
mtcars
```
**Exercise**: First, summarise the average miles/gallon (mpg) across the entire dataset.
```{r}
```
**Exercise**: A car can either have 4, 6, or 8 cylinders (cyl). Summarise the average mpg, broken down by the number of cylinders. Hint: You may want to "group" by cyl in order to do this.
```{r}
```
**Exercise**: In addition to the means, add standard deviations to this summary (still grouped by cyl).
```{r}
```
**BONUS**: Let's visualize! Use ggplot (included in the tidyverse package) to make a scatter plot of mpg by horsepower. If you are feeling extra fancy, you can add a smoothing line. (Hint: Google "geom_smooth() scatterplot".)
```{r}
```
# Reshaping with tidyr
## From long to wide and back again
We will first use a built-in table in package `tidyr`: table3. We can use `help(table3)` to find its information.
```{r}
```
`table3` is in tidy format. Make this into wide data.
```{r}
```
Now make it back into tidy data.
```{r}
```
Here are examples of more recently published functions for wide to long or long to wide. These two functions have more straightforward names and argument names, which makes them easier to use.
```{r}
```
## From wide to long without seeing the tidy version
These are pre-post data on children's arithmetic scores from a RCT (Randomized Controlled Trial) in which they were assigned either to CNTL (control) or MA (mental abacus math intervention). They were tested twice, once in 2015 and once in 2016. The paper can be found at https://jnc.psychopen.eu/article/view/106.
```{r}
```
Make these tidy.
```{r}
```
**OPTIONAL**: make these back to wide format.
```{r}
```