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.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.
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.
## 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
## Warning: package 'gapminder' was built under R version 4.3.3
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.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.
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.
## 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…
Arrange the gapminderdataset in both ascending and
descending order based on pop variable. Identify the
countries which have the highest and lowest population.
Filter the gapminderdataset for European countries and
in the year 1997 and identify the first two countries which have the
highest life expectancy.
## 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…
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))Calculate the average gdpPercap for all continents,
countries and years. Note that you can write 3 separate codes for the
three requirements.
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")'
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.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `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.