-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path24-tidying.Rmd
More file actions
89 lines (54 loc) · 3.54 KB
/
Copy path24-tidying.Rmd
File metadata and controls
89 lines (54 loc) · 3.54 KB
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
# Tidying {#tidying}
## Setup
```{r}
library(tidyverse)
```
## References of `tidyr`
* Textbook: [R for Data Science,Tidy Data](https://r4ds.had.co.nz/tidy-data.html#tidy-data)
### RStudio Primers: See References in Moodle at the bottom
4. Tidy Your Data -- [r4ds: Wrangle, II](https://r4ds.had.co.nz/wrangle-intro.html#wrangle-intro)
- [Reshape Data - a bit old](https://rstudio.cloud/learn/primers/4.1)
- [Separate and Unite](https://rstudio.cloud/learn/primers/4.2)
- [Join Data Sets](https://rstudio.cloud/learn/primers/4.3)
The first component, 'Reshape Data' deals with `pivot_longer` and `pivot_wider`. However, it uses an older version of these functions calls `gather` and `spread`.
## Variables, values, and observations: Definitions
* A **variable** is a quantity, quality, or property that you can measure.
* A **value** is the state of a variable when you measure it. The value of a variable may change from measurement to measurement.
* An **observation** or **case** is a set of measurements made under similar conditions (you usually make all of the measurements in an observation at the same time and on the same object). An observation will contain several values, each associated with a different variable. I’ll sometimes refer to an observation as a case or data point.
* **Tabular data** is a table of values, each associated with a variable and an observation. Tabular data is tidy if each value is placed in its own cell, each variable in its own column, and each observation in its own row.
* So far, all of the data that you’ve seen has been tidy. In real-life, most data isn’t tidy, so we’ll come back to these ideas again in Data Wrangling.
## Tidy Data
> “Data comes in many formats, but R prefers just one: tidy data.” — Garrett Grolemund
Data can come in a variety of formats, but one format is easier to use in R than the others. This format is known as tidy data. A data set is tidy if:
1. Each variable is in its own column
2. Each observation is in its own row
3. Each value is in its own cell (this follows from #1 and #2)
> “Tidy data sets are all alike; but every messy data set is messy in its own way.” — Hadley Wickham
> “all happy families are all alike; each unhappy family is unhappy in its own way” - Tolstoy's Anna Karenina
## `tidyr` Basics
Let us look at the figure in [R4DS](https://d33wubrfki0l68.cloudfront.net/6f1ddb544fc5c69a2478e444ab8112fb0eea23f8/91adc/images/tidy-1.png).
```{r, echo=FALSE, out.width="100%"}
knitr::include_graphics("./data/tidy-1.png")
```
1. Each variable is in its own column
2. Each observation is in its own row
## Pivot data from wide to long:
[`pivot_longer()`](https://tidyr.tidyverse.org/reference/pivot_longer.html)
```
pivot_longer(data, cols = <columns to pivot into longer format>,
names_to = <name of the new character column>, # e.g. "group", "category", "class"
values_to = <name of the column the values of cells go to>) # e.g. "value", "n"
```
## Pivot data from long to wide:
[`pivot_wider()`](https://tidyr.tidyverse.org/reference/pivot_wider.html)
In Console: vignette("pivot")
```
pivot_wider(data,
names_from = <name of the column (or columns) to get the name of the output column>,
values_from = <name of the column to get the value of the output>)
```
## Filtering joins
[`anti_join()`, `semi_join()`](https://dplyr.tidyverse.org/reference/filter-joins.html)
* `anti_join(x,y, ...)`: return all rows from x without a match in y.
* `semi_join(x,y, ...)`: return all rows from x with a match in y.
Check `dplyr` cheat sheet, and Posit Primers Tidy Data.