Adding results to databases

Up to this point, we have simply manipulated the data other people collected. What if we now want to do our own analysis with such data? Piece of cake…

…lets tray, for instance, a new index, in which I want to see how much does it cost a year of life in each country. Basically, all I have to do is to divide how much each person does in each country (i.e., GDP per capita) by the life expectancy of people in each country. Like this,

SelectedColumns$ValueofLife = SelectedColumns$gdpPercap / SelectedColumns$lifeExp 

WE cant translate the code above as, a a column called ValueofLife to the dataframe SelectedColumns, in which I divide gdpPercap by lifeExp.

Lets check the results,

head(SelectedColumns)
##       country year gdpPercap lifeExp ValueofLife
## 1 Afghanistan 1952  779.4453  28.801    27.06313
## 2 Afghanistan 1957  820.8530  30.332    27.06228
## 3 Afghanistan 1962  853.1007  31.997    26.66190
## 4 Afghanistan 1967  836.1971  34.020    24.57957
## 5 Afghanistan 1972  739.9811  36.088    20.50491
## 6 Afghanistan 1977  786.1134  38.438    20.45146

Lets do a quiick summary to see the average per country,

SelectedColumns %>% group_by(country) %>% summarize(mean_valueOfLife = mean(ValueofLife))
## # A tibble: 142 × 2
##    country     mean_valueOfLife
##    <chr>                  <dbl>
##  1 Afghanistan             21.8
##  2 Albania                 46.7
##  3 Algeria                 73.7
##  4 Angola                  97.2
##  5 Argentina              129. 
##  6 Australia              263. 
##  7 Austria                273. 
##  8 Bahrain                271. 
##  9 Bangladesh              16.3
## 10 Belgium                266. 
## # ℹ 132 more rows

Have a look at the result above. Isn’t it fascinating how in Afghanistan we could increase the life expectancy by one year by simply having people making $21.8 dollars more!