-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlong_to_wide_and_back.Rmd
executable file
·68 lines (50 loc) · 1.3 KB
/
long_to_wide_and_back.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
---
title: "long to wide or wide to long"
author: "Evangeline Reynolds"
date: "2/16/2019"
output:
xaringan::moon_reader:
chakra: libs/remark-latest.min.js
lib_dir: libs
css: [default, hygge, ninjutsu]
nature:
ratio: 16:10
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r}
library(gapminder)
library(tidyverse)
library(flipbookr)
knitr::opts_chunk$set(cache = F, comment = "")
```
---
name: longtowideandback
## Exercise: *spread data, gather data*
Sometimes, we need to transform the shape of our data. The wide form of data is an efficient way to store data - especially time-series data. However, we often need data in a long for for data analysis, so shape transformations are important.
---
## Exercise: *long to wide*
---
```{r long_to_wide, include = F}
gapminder %>%
select(country, continent,
lifeExp, year) %>%
spread(key = year,
value = lifeExp) ->
gapminder_life_exp_wide
```
`r chunk_reveal("long_to_wide")`
---
## Exercise: *wide to long*
---
```{r wide_to_long, include = F}
gapminder_life_exp_wide %>%
gather(key = "year",
value = "lifeExp",
-country, -continent) %>%
mutate(year = as.numeric(year)) ->
gapminder_life_exp_long
```
`r chunk_reveal("wide_to_long")`
---