-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbonus_tidy_gapminder.R
More file actions
58 lines (47 loc) · 1.4 KB
/
Copy pathbonus_tidy_gapminder.R
File metadata and controls
58 lines (47 loc) · 1.4 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
##########################################################################
# Jose Cajide - @jrcajide
# Master Data Science: Tidyr
##########################################################################
library(tidyverse)
library(readr)
library(tidyr)
gapminder <- read_csv("data/gapminder.csv")
gap_wide <- read_csv("https://docs.google.com/spreadsheets/d/1NTXQNoY8V0H_EZ_peFmnH1ZcGlxCPhwl2VmJNpiACMU/pub?output=csv")
# gap_wide <- read.csv('data/gapminder_wide.csv')
head(gap_wide)
gap_long <- gap_wide %>%
gather(key = obstype_year,
value = obs_values)
head(gap_long)
tail(gap_long)
# Debemos indicar las columnas a transformar
gap_long <- gap_wide %>%
gather(key = obstype_year,
value = obs_values,
3:38) # ó -1:-2
head(gap_long)
tail(gap_long)
# Alternativa
gap_long <- gap_wide %>%
gather(key = obstype_year,
value = obs_values,
dplyr::starts_with('pop'),
dplyr::starts_with('lifeExp'),
dplyr::starts_with('gdpPercap'))
head(gap_long)
tail(gap_long)
# ¿Que ocurre con la variable 'obstype_year'?
gap_long <- gap_wide %>%
gather(key = obstype_year,
value = obs_values,
-continent, -country) %>%
separate(obstype_year,
into = c('obs_type','year'),
sep="_")
head(gap_long)
tail(gap_long)
# spread()
gap_normal <- gap_long %>%
spread(obs_type, obs_values)
head(gap_normal)
head(gapminder)