Lastname_Firstname_Week1.html (or
.pdf) and upload.Task A1. Run the following commands and briefly describe what each does.
5 + 7
## [1] 12
sqrt(144)
## [1] 12
log(10)
## [1] 2.302585
Your notes (1–2 sentences):
5 + 7 → 12, This is a simple summation adding 5 and
7.sqrt(144) → 12, This operation is the principal square
root of a perfect squarelog(10) → 2.302585, This operation is the natural
logarithm of tenTask B1. Create a numeric vector of five values (any values) and compute the mean, min, and max.
# Your code here
vec <- c(2,5,8,10,12)
mean(vec)
## [1] 7.4
min(vec)
## [1] 2
max(vec)
## [1] 12
Task B2. Create a character vector of three country names and return the second element.
# Your code here
countries <- c("Persia", "Yugoslavia", "Zanzibar")
countries[2]
## [1] "Yugoslavia"
Task B3. Create a logical vector of five values and count how many are TRUE.
# Your code here
flags <- c(TRUE, FALSE, TRUE, FALSE, TRUE)
sum(flags) # TRUE counts as 1
## [1] 3
We’ll use a lightweight dataset:
Task C1. Import the CSV with
readr::read_csv() and display the first rows and the
structure.
library(readr)
Gapminder <- read_csv("https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv")
head(Gapminder);str(Gapminder)
## spc_tbl_ [1,704 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ country : chr [1:1704] "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
## $ year : num [1:1704] 1952 1957 1962 1967 1972 ...
## $ pop : num [1:1704] 8425333 9240934 10267083 11537966 13079460 ...
## $ continent: chr [1:1704] "Asia" "Asia" "Asia" "Asia" ...
## $ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ...
## $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
## - attr(*, "spec")=
## .. cols(
## .. country = col_character(),
## .. year = col_double(),
## .. pop = col_double(),
## .. continent = col_character(),
## .. lifeExp = col_double(),
## .. gdpPercap = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
Task C2. How many rows and columns are there?
# Add your code
#Tibble description shows: "spc_tbl_ [1,704 × 6]"
#This indicates there are 1,704 rows (Observations) and 6 columns (Variables)
#We can confirm this with the following code:
nrow(Gapminder)
## [1] 1704
ncol(Gapminder)
## [1] 6
Task C3. List the continents represented (unique values).
# Add your code
length(unique(Gapminder$continent))
## [1] 5
Task C4. Create a quick frequency table of countries by continent (counts only).
# Add your code
table(Gapminder$continent)
##
## Africa Americas Asia Europe Oceania
## 624 300 396 360 24
Task D1. Filter to the year 2007 and compute the mean life expectancy across all countries.
# Add your code
mean(Gapminder$lifeExp[Gapminder$year == "2007"], na.rm = TRUE)
## [1] 67.00742
Task D2. Show a small summary table of mean life expectancy by continent (year 2007).
# Add your code
aggregate(lifeExp ~ continent, data = Gapminder[Gapminder$year == 2007, ], FUN = mean)
Task D3. In 2–3 sentences,
interpret the 2007 results in a public health context.
What patterns (inequities, regional differences) do you notice?
We observe the African continent to have the lowest life expectancy (54.81) in 2007 while Oceania enjoys the highest (80.72 years). The continents between the maximum and minimum are closer within ranges with Asia (70.73), Americas (73.61), and Europe (77.69). We can add supplemental variables that may help clarify this disparity: GDP per Capita, population We notice a similar trend: increase in GDP per Capita appears to follow a similar trend to life expectancy, with the exception of Americas and Asia. We can also notice a smaller population following a inverse pattern with life expectancy. Individual similarities in pattern of pop and gdp to life expectancy can be determined with statistical testing, and combined contributions can be evaluated with a regression.
# Add your code
aggregate(cbind(lifeExp, gdpPercap, pop) ~ continent, data = Gapminder[Gapminder$year == 2007, ], FUN = mean)