Q1

data(cars)
median(cars$speed)
## [1] 15

Q2

# Load required package
library(jsonlite)

# Define the API URL (Daily OHLCV for BTC in USD, last 100 days)
url <- "https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&limit=100"

# Read the JSON data
btc_data <- fromJSON(url)

# Extract the 'Data' part
btc_df <- btc_data$Data$Data

# Find the maximum daily close price
max_close <- max(btc_df$close, na.rm = TRUE)

# Print the result
max_close
## [1] 124723
# Load required packages
library(WDI)
library(dplyr)

Internet Access, Wealth, and Life Expectancy Around the World

# Select indicators
ind <- c(
  "NY.GDP.PCAP.KD",   # GDP per capita (constant 2015 US$)
  "IT.NET.USER.ZS",   # Internet users (% of population)
  "SP.DYN.LE00.IN"    # Life expectancy (years)
)

# Download data
dat_raw <- WDI(country = "all", indicator = ind, start = 2000, end = 2023, extra = TRUE)

# Clean and rename
dat <- dat_raw |>
  dplyr::filter(region != "Aggregates") |>
  dplyr::rename(
    gdp = NY.GDP.PCAP.KD,
    internet = IT.NET.USER.ZS,
    life = SP.DYN.LE00.IN
  )

Research Question 1

What percentage of the world uses the internet today?

# Find the most recent year
latest_year <- max(dat$year, na.rm = TRUE)

# Calculate average internet usage for that year
avg_internet <- mean(dat$internet[dat$year == latest_year], na.rm = TRUE)

# Print result
cat("In", latest_year, "the average percent of internet users was about",
    round(avg_internet, 1), "%.")
## In 2023 the average percent of internet users was about 72.3 %.

Research Question 2

Is wealth associated with internet access?

# Find the most recent year
latest_year <- max(dat$year, na.rm = TRUE)

# Keep only data from that year
data_latest <- dat[dat$year == latest_year, ]

# Calculate correlation between GDP per capita and internet use
correlation <- cor(data_latest$gdp, data_latest$internet, use = "complete.obs")

# Print result
cat("In", latest_year, "the correlation between GDP per capita and internet usage was",
    round(correlation, 3))
## In 2023 the correlation between GDP per capita and internet usage was 0.502

Research Question 3

Which income group lives the longest?

# Find the most recent year
latest_year <- max(dat$year, na.rm = TRUE)

# Keep only that year and drop missing values
data_latest <- dat[dat$year == latest_year, c("income", "life")]
data_latest <- data_latest[!is.na(data_latest$income) & !is.na(data_latest$life), ]

# Calculate average life expectancy by income group
life_by_income <- aggregate(life ~ income, data = data_latest, FUN = mean)

# Show table
print(life_by_income)
##                income     life
## 1         High income 79.79566
## 2          Low income 64.35928
## 3 Lower middle income 68.70162
## 4      Not classified 72.51400
## 5 Upper middle income 73.81495
# Identify the group with the highest average life expectancy
best <- life_by_income[which.max(life_by_income$life), ]

# Print result
cat("In", latest_year, "the income group with the highest average life expectancy was",
    best$income, "with", round(best$life, 1), "years.")
## In 2023 the income group with the highest average life expectancy was High income with 79.8 years.

Research Question 4

Who improved internet access the most since 2000?

# Define the first and latest years
first_year <- 2000
latest_year <- max(dat$year, na.rm = TRUE)

# Get data for both years
first_data <- dat[dat$year == first_year, c("country", "internet")]
last_data  <- dat[dat$year == latest_year, c("country", "internet")]

# Merge the two datasets by country
merged <- merge(first_data, last_data, by = "country")

# Rename columns
colnames(merged) <- c("country", "internet_2000", "internet_latest")

# Calculate the change in internet usage
merged$change <- merged$internet_latest - merged$internet_2000

# Sort from biggest to smallest increase
merged <- merged[order(-merged$change), ]

# Show the top 10 countries with the largest improvement
head(merged, 10)
##                country internet_2000 internet_latest   change
## 166       Saudi Arabia      2.210690        100.0000 97.78931
## 159              Qatar      4.863680         99.6528 94.78912
## 15             Bahrain      6.153730        100.0000 93.84627
## 107             Kuwait      6.731400         99.7473 93.01590
## 101         Kazakhstan      0.668594         92.8785 92.20991
## 149               Oman      3.520420         95.2517 91.73128
## 134            Morocco      0.693791         91.0000 90.30621
## 161 Russian Federation      1.977230         92.2450 90.26777
## 29   Brunei Darussalam      8.996280         99.0336 90.03732
## 100             Jordan      2.623280         92.5344 89.91112

Research Question 5

Do countries with higher life expectancy also have more internet users?

# Find the most recent year
latest_year <- max(dat$year, na.rm = TRUE)

# Keep only that year and drop missing values
data_latest <- dat[dat$year == latest_year, c("life", "internet")]
data_latest <- data_latest[!is.na(data_latest$life) & !is.na(data_latest$internet), ]

# Calculate correlation between life expectancy and internet usage
correlation <- cor(data_latest$life, data_latest$internet)

# Print result
cat("In", latest_year, "the correlation between life expectancy and internet usage was",
    round(correlation, 3))
## In 2023 the correlation between life expectancy and internet usage was 0.808