Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# reshape child_mortality data frame
child_mortality <- melt(child_mortality, id.vars="Country", variable.name = "Years", value.name="Child mortality", na.rm = TRUE)
str(child_mortality)
## 'data.frame': 33490 obs. of 3 variables:
## $ Country : chr "Sweden" "Sweden" "Sweden" "Sweden" ...
## $ Years : Factor w/ 253 levels "1761","1762",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ Child mortality: num 316 377 395 316 316 ...
# change factor to numeric
child_mortality$Years <- as.numeric(as.character(child_mortality$Years))
str(child_mortality)
## 'data.frame': 33490 obs. of 3 variables:
## $ Country : chr "Sweden" "Sweden" "Sweden" "Sweden" ...
## $ Years : num 1761 1762 1763 1764 1765 ...
## $ Child mortality: num 316 377 395 316 316 ...
head(child_mortality)
## Country Years Child mortality
## 221 Sweden 1761 315.61
## 482 Sweden 1762 376.98
## 743 Sweden 1763 395.31
## 1004 Sweden 1764 316.08
## 1265 Sweden 1765 316.03
## 1526 Sweden 1766 311.90
# reshape democracy_score data frame
democracy_score <- melt(democracy_score, id.vars="Country", variable.name = "Years", value.name="Democracy Score", na.rm = TRUE)
str(democracy_score)
## 'data.frame': 17798 obs. of 3 variables:
## $ Country : chr "Afghanistan" "Austria" "China" "Denmark" ...
## $ Years : Factor w/ 212 levels "1800","1801",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Democracy Score: num -6 -10 -6 -10 -8 -10 -10 1 -6 -6 ...
# change factor to numeric
democracy_score$Years <- as.numeric(as.character(democracy_score$Years))
str(democracy_score)
## 'data.frame': 17798 obs. of 3 variables:
## $ Country : chr "Afghanistan" "Austria" "China" "Denmark" ...
## $ Years : num 1800 1800 1800 1800 1800 1800 1800 1800 1800 1800 ...
## $ Democracy Score: num -6 -10 -6 -10 -8 -10 -10 1 -6 -6 ...
head(democracy_score)
## Country Years Democracy Score
## 2 Afghanistan 1800 -6
## 15 Austria 1800 -10
## 45 China 1800 -6
## 60 Denmark 1800 -10
## 77 France 1800 -8
## 104 Iran 1800 -10
# reshape life_expectancy data frame
life_expectancy <- melt(life_expectancy, id.vars="Country", variable.name = "Years", value.name="Life Expectancy", na.rm = TRUE)
str(life_expectancy)
## 'data.frame': 44359 obs. of 3 variables:
## $ Country : chr "Denmark" "Finland" "France" "Iceland" ...
## $ Years : Factor w/ 254 levels "1765","1766",..: 1 1 1 1 1 1 1 2 2 2 ...
## $ Life Expectancy: num 33.3 35.3 27 40.1 35 ...
# change factor to numeric
life_expectancy$Years <- as.numeric(as.character(life_expectancy$Years))
str(life_expectancy)
## 'data.frame': 44359 obs. of 3 variables:
## $ Country : chr "Denmark" "Finland" "France" "Iceland" ...
## $ Years : num 1765 1765 1765 1765 1765 ...
## $ Life Expectancy: num 33.3 35.3 27 40.1 35 ...
head(life_expectancy)
## Country Years Life Expectancy
## 57 Denmark 1765 33.32457
## 71 Finland 1765 35.32585
## 72 France 1765 26.95198
## 96 Iceland 1765 40.07231
## 163 Norway 1765 34.98737
## 210 Sweden 1765 35.95000
# reshape population data frame
population <- melt(population, id.vars="Country", variable.name = "Years", value.name="Population", na.rm = TRUE)
str(population)
## 'data.frame': 21591 obs. of 3 variables:
## $ Country : chr "Albania" "Algeria" "American Samoa" "Australia" ...
## $ Years : Factor w/ 232 levels "1700","1730",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Population: chr "300,000" "1,750,000" "7,427" "450,000" ...
# change factor to numeric
population$Years <- as.numeric(as.character(population$Years))
# population value has comma separators which need to be removed
population$Population <- as.numeric(gsub(',', '', population$Population))
str(population)
## 'data.frame': 21591 obs. of 3 variables:
## $ Country : chr "Albania" "Algeria" "American Samoa" "Australia" ...
## $ Years : num 1700 1700 1700 1700 1700 1700 1700 1700 1700 1700 ...
## $ Population: num 300000 1750000 7427 450000 2500000 ...
head(population)
## Country Years Population
## 4 Albania 1700 300000
## 5 Algeria 1700 1750000
## 6 American Samoa 1700 7427
## 14 Australia 1700 450000
## 15 Austria 1700 2500000
## 19 Bangladesh 1700 15789473
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment