Skip to content

Instantly share code, notes, and snippets.

@davidcomfort
Created November 15, 2015 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidcomfort/5435b4b030fc8a810d9b to your computer and use it in GitHub Desktop.
Save davidcomfort/5435b4b030fc8a810d9b to your computer and use it in GitHub Desktop.
# Get an idea if it will be correct join
head(left_join(gdp_per_capita, child_mortality, by=c("Country", "Years")))
## Country Years GDP per capita Child mortality
## 1 Afghanistan 1800 634.4000 468.58
## 2 Albania 1800 860.5880 375.20
## 3 Algeria 1800 1360.0000 460.21
## 4 Andorra 1800 1260.0000 NA
## 5 Angola 1800 650.0000 485.68
## 6 Antigua and Barbuda 1800 796.5934 473.60
# perform the left join
gapdata <- left_join(gdp_per_capita, child_mortality, by=c("Country","Years"))
head(gapdata)
## Country Years GDP per capita Child mortality
## 1 Afghanistan 1800 634.4000 468.58
## 2 Albania 1800 860.5880 375.20
## 3 Algeria 1800 1360.0000 460.21
## 4 Andorra 1800 1260.0000 NA
## 5 Angola 1800 650.0000 485.68
## 6 Antigua and Barbuda 1800 796.5934 473.60
# Join the democracy_score data frame
gapdata <- left_join(gapdata, democracy_score, by=c("Country", "Years"))
# Join the life_expectancy data frame
gapdata <- left_join(gapdata, life_expectancy, by=c("Country", "Years"))
# Join the population data frame
gapdata <- left_join(gapdata, population, by=c("Country", "Years"))
str(gapdata)
## 'data.frame': 43252 obs. of 7 variables:
## $ Country : chr "Afghanistan" "Albania" "Algeria" "Andorra" ...
## $ Years : num 1800 1800 1800 1800 1800 1800 1800 1800 1800 1800 ...
## $ GDP per capita : num 634 861 1360 1260 650 ...
## $ Child mortality: num 469 375 460 NA 486 ...
## $ Democracy Score: num -6 NA NA NA NA NA NA NA NA NA ...
## $ Life Expectancy: num 28.2 35.4 28.8 NA 27 ...
## $ Population : num 3280000 410445 2503218 2654 1567028 ...
# join the gapdata and countries data frame
# see if we are doing the right thing
head(left_join(gapdata, countries, by="Country"))
## Country Years GDP per capita Child mortality Democracy Score
## 1 Afghanistan 1800 634.4000 468.58 -6
## 2 Albania 1800 860.5880 375.20 NA
## 3 Algeria 1800 1360.0000 460.21 NA
## 4 Andorra 1800 1260.0000 NA NA
## 5 Angola 1800 650.0000 485.68 NA
## 6 Antigua and Barbuda 1800 796.5934 473.60 NA
## Life Expectancy Population Code Region Sub.Region
## 1 28.2110 3280000 4 Asia Southern Asia
## 2 35.4000 410445 8 Europe Southern Europe
## 3 28.8224 2503218 12 Africa Northern Africa
## 4 NA 2654 20 Europe Southern Europe
## 5 26.9800 1567028 24 Africa Middle Africa
## 6 33.5360 37000 28 Americas Caribbean
gapdata <- left_join(gapdata, countries, by=c("Country"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment