forked from EvaMaeRey/tidyverse_in_action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoining_data.Rmd
executable file
·80 lines (55 loc) · 1.48 KB
/
joining_data.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
---
title: "joining data"
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: joiningdata
## Exercise: *spread data, gather data*
Sometimes we'll need to join diverse data together from different sources. Here let's create a contrived example. We'll pretend that the data comes from various sources
---
## Exercise: *Create dataset 1 for contrived example*
---
```{r dataset_1, include = F}
gapminder %>%
select(country, lifeExp, year) ->
country_life_exp
```
`r chunk_reveal("dataset_1")`
---
## Exercise: *Create dataset 2 for contrived example*
---
```{r dataset_2, include = F}
gapminder %>%
select(country, pop, year) ->
country_pop
```
`r chunk_reveal("dataset_2")`
---
## Exercise: *Join contrived datasets 1 and 2*
A natural join merges on any variable names found in common between the data sets and occurs when the by argument is not specified. Here we'll just be explicit about how to join the two dataframes.
---
```{r merging_data, include = F}
country_pop %>%
full_join(country_life_exp,
by = c("country", "year")) ->
merged_data
```
`r chunk_reveal("merging_data")`
---