Instructions for the Data Analytics (DA) Projects :

  1. If because of technological issues or any other issues, you cannot submit the project, then you will get 0. It is your responsibility to submit the project on time. Therefore, please take all necessary steps so that the project can be turned in on time.
  2. Please note that you MUST upload the html file on D2L (Not the code file); however, you MUST keep a copy of code file with you because you might be requested to turn in your code file. If you are requested to turn in code file and you fail to do so, you will get 0.
  3. This is RMarkdown file, which is different from R Script (we use R Script in class room practice). In order to write code in RMarkdown file, please press ctrl + alt + I for Windows OR Cmd + Option + I for macOS , then you will see space between two-set of 03 backtick (which is also called backquote and you will find it tilde (~) button on your keyboard). Please write your code between the space of two-set of 03 dots.

Loading the Data Set

Please install and load the gapminder package. Please note that the name of the data set is also gapminder. Also note that you also need to load the tidyverse package if you want to use the six (06) verbs from dplyr package.

1 Question # 1

Please write some information about metadata about gapminder data. Don’t forget to mention the number of rows (observations) and columns (variables) of the data set. Also mention the type of the variables - country and year.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'tibble' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'purrr' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.3.3
gapminder <- gapminder
view(gapminder)
#Gapminder has 1704 rows(observations) and 6 columns (variables). The variables include country, continent,year, life expectancy, population and GDP per cap.

2 Question # 2

Please subset the gapminderdataset for the country United States and give the name of the data set US. Note that do not forget to discuss the meta data of US data set.

library(tidyverse)
library(gapminder)
US <- gapminder %>% filter(country=="United States")
glimpse(US)
## Rows: 12
## Columns: 6
## $ country   <fct> "United States", "United States", "United States", "United S…
## $ continent <fct> Americas, Americas, Americas, Americas, Americas, Americas, …
## $ year      <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp   <dbl> 68.440, 69.490, 70.210, 70.760, 71.340, 73.380, 74.650, 75.0…
## $ pop       <int> 157553000, 171984000, 186538000, 198712000, 209896000, 22023…
## $ gdpPercap <dbl> 13990.48, 14847.13, 16173.15, 19530.37, 21806.04, 24072.63, …
# The US subset shows the GDP per capita, population and life expectancy in the US. It starts at 1957 and goes up to 2007. All of these variables steadily increased over the years. We see that in 1957 the life expectancy was around 68 years and in 2007 it is about a decade longer at around 78 years. Population in 1957 was 157553000 and steadily increased until the population grew to 301139947.The GDPpercap variable started out at 13990.48 in 1957 and then steadily grew to 42951.65 in 2007.

3 Question # 3

Filter the gapminderdataset for all countries except Europe and name the data set not_europe. Note that do not forget to discuss the meta data of not_europe data set.

not_europe <- gapminder %>%filter (continent!="Europe")
#Once all European countries are removed from the data set the number of rows(observations) is reduced by 360 to 1344 rows(observations). The number of variables stays the same with the European information removed.

4 Question # 4

Filter the gapminderdataset for the year 1992 and gdpPercap >= 5000 and name the data set sample and discuss the meta data of this new dataset.

sample <- gapminder %>% filter (year==1992, gdpPercap>=5000)
glimpse(sample)
## Rows: 67
## Columns: 6
## $ country   <fct> "Algeria", "Argentina", "Australia", "Austria", "Bahrain", "…
## $ continent <fct> Africa, Americas, Oceania, Europe, Asia, Europe, Africa, Ame…
## $ year      <int> 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, …
## $ lifeExp   <dbl> 67.744, 71.868, 77.560, 76.040, 72.601, 76.460, 62.745, 67.0…
## $ pop       <int> 26298373, 33958947, 17481977, 7914969, 529491, 10045622, 134…
## $ gdpPercap <dbl> 5023.217, 9308.419, 23424.767, 27042.019, 19035.579, 25575.5…
#Once the data set is filtered to only show data that is from the year 1992 and has a gdpPercap of 5000 or greater there is only 67 columns(observations).

5 Question # 5

Arrange the gapminderdataset in both ascending and descending order based on pop variable. Identify the countries which have the highest and lowest population.

gapminder %>% arrange(pop)
gapminder %>% arrange(desc(pop))
# The country with the lowest population is Sao Tome and Principe in Africa with a population of 60,011 in 1952. The country with the highest population is China in Asia with a population of 1318683096 in 2007.

6 Question # 6

Filter the gapminderdataset for European countries and in the year 1997 and identify the first two countries which have the highest life expectancy.

Europe_1997 <- gapminder %>% filter(continent=="Europe", year==1997)
glimpse(Europe_1997)
## Rows: 30
## Columns: 6
## $ country   <fct> "Albania", "Austria", "Belgium", "Bosnia and Herzegovina", "…
## $ continent <fct> Europe, Europe, Europe, Europe, Europe, Europe, Europe, Euro…
## $ year      <int> 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, …
## $ lifeExp   <dbl> 72.950, 77.510, 77.530, 73.244, 70.320, 73.680, 74.010, 76.1…
## $ pop       <int> 3428038, 8069876, 10199787, 3607000, 8066057, 4444595, 10300…
## $ gdpPercap <dbl> 3193.055, 29095.921, 27561.197, 4766.356, 5970.389, 9875.605…
Europe_1997 %>%  arrange(desc(lifeExp))
#In 1997 Sweden and Switzerland had the highest life expectancy's. The life expectancy in both countries is very close with Sweden at 79.39 and Switzerland just barely behind at 79.37.

7 Question # 7

Calculate 2 new variables called gdp, which is the product of two variables - gdpPercap and pop and LifeEarn, which is the product of two variables - gdpPercap and lifeExpand the name the data set new_data. Also arrange the new_data in descending order based on gdp - the new variable that you just created.

new_data <- gapminder %>% mutate(gdp=gdpPercap*pop, LifeEarn=gdpPercap*lifeExp)
new_data %>% arrange(desc(gdp))

8 Question # 8

Calculate the average gdpPercap for all continents, countries and years. Note that you can write 3 separate codes for the three requirements.

gapminder %>% group_by(continent) %>% summarise(avg_gdpPercap=mean(gdpPercap,
 na.rm = TRUE))
gapminder %>% group_by(year) %>% summarise(avg_gdpPercap=mean(gdpPercap,
  na.rm = TRUE))

9 Question # 9

Draw a line plot for gdpPercap and year, where year is in x axis and gdpPercap is in y axis. Also comment about what you can identify from this visualization.

library(gapminder)
library(ggplot2)
gapminder %>% ggplot(mapping = aes(x=year, y=gdpPercap))+geom_smooth()
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

#This plot shows that there is a positive relationship between year and gdpPercap. #As the years go on the gdpPercap increases.

10 Question # 10

Visualize the gdpPercap of different continent in different year and do the same thing for lifeExp different continent in different year. Using this two graphs, comment how the income and life expectancy changes/transforms in different continents in different years.

gapminder %>% ggplot(mapping=aes(y=gdpPercap, x=year, color=continent))+ geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

gapminder %>% ggplot(mapping=aes(y=lifeExp, x=year, color=continent))+ geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

#GDP per cap increased rapidly over the years in Europe and Oceania. The Americas and Asia had a slight decline in the mid 1980s but did start increasing again. Africas GDPpercap stayed around the same over the years with slight increases and decreases in some of the years. For life expectancy most all of the continents consistently increased over the years. Africa however increased until about the mid 1980s and then the life expectancy remained about the same. 
GOOD LUCK!