The built-in cars dataset contains the speed and
stopping distances of cars.
Here we compute the median of the first column (speed):
# Load cars dataset
data(cars)
# Compute median speed
median_speed <- median(cars[, 1])
median_speed
## [1] 15
Answer: The median of the first column is 15.
We will retrieve daily BTC–USD price data for the
past 100 days from CryptoCompare.
The data is in JSON format, so we will use
jsonlite::fromJSON().
# Load required package
library(jsonlite)
# Construct the URL for 100 days of BTC-USD daily data
url <- "https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&limit=100"
# Retrieve JSON data
btc_raw <- fromJSON(url)
# Inspect structure
str(btc_raw, max.level = 2)
## List of 6
## $ Response : chr "Success"
## $ Message : chr ""
## $ HasWarning: logi FALSE
## $ Type : int 100
## $ RateLimit : Named list()
## $ Data :List of 4
## ..$ Aggregated: logi FALSE
## ..$ TimeFrom : int 1751155200
## ..$ TimeTo : int 1759795200
## ..$ Data :'data.frame': 101 obs. of 9 variables:
The price data is stored under btc_raw$Data$Data.
We’ll extract it into a clean data frame and compute the maximum
daily close price.
# Extract the data sublist
btc_df <- btc_raw$Data$Data
# Convert timestamp to Date format
btc_df$time <- as.POSIXct(btc_df$time, origin = "1970-01-01")
btc_df$date <- as.Date(btc_df$time)
# Look at first few rows
head(btc_df)
## time high low open volumefrom volumeto close
## 1 2025-06-28 20:00:00 108538.4 107233.6 107346.4 6094.35 657403121 108391.9
## 2 2025-06-29 20:00:00 108815.7 106756.4 108391.9 14220.51 1530756559 107169.8
## 3 2025-06-30 20:00:00 107574.1 105295.0 107169.8 15730.48 1673173145 105724.2
## 4 2025-07-01 20:00:00 109818.5 105143.1 105724.2 19611.45 2124516059 108886.6
## 5 2025-07-02 20:00:00 110584.4 108579.6 108886.6 16647.63 1825182142 109639.0
## 6 2025-07-03 20:00:00 109810.0 107283.9 109639.0 10440.09 1130647601 108027.7
## conversionType conversionSymbol date
## 1 direct 2025-06-29
## 2 direct 2025-06-30
## 3 direct 2025-07-01
## 4 direct 2025-07-02
## 5 direct 2025-07-03
## 6 direct 2025-07-04
Now compute the maximum closing price:
max_close <- max(btc_df$close, na.rm = TRUE)
max_close
## [1] 124723
Answer: The maximum daily close price for BTC over the past 100 days is 1.24723^{5} USD.
Project Title: Bitcoin Price Trends — Last 100
Days
We explore daily BTC price behavior using data from CryptoCompare.
We have already extracted 100-day BTC data using the CryptoCompare
API in Q2.
We will reuse btc_df and clean it for analysis.
# Keep relevant columns
btc_clean <- btc_df[, c("date", "open", "high", "low", "close", "volumefrom", "volumeto")]
# Check structure
str(btc_clean)
## 'data.frame': 101 obs. of 7 variables:
## $ date : Date, format: "2025-06-29" "2025-06-30" ...
## $ open : num 107346 108392 107170 105724 108887 ...
## $ high : num 108538 108816 107574 109818 110584 ...
## $ low : num 107234 106756 105295 105143 108580 ...
## $ close : num 108392 107170 105724 108887 109639 ...
## $ volumefrom: num 6094 14221 15730 19611 16648 ...
## $ volumeto : num 6.57e+08 1.53e+09 1.67e+09 2.12e+09 1.83e+09 ...
# Sort by date just to be sure
btc_clean <- btc_clean[order(btc_clean$date), ]
We’ll calculate basic statistics and daily returns for further analysis.
# Basic descriptive statistics
summary_stats <- data.frame(
Min_Close = min(btc_clean$close),
Max_Close = max(btc_clean$close),
Mean_Close = mean(btc_clean$close),
Median_Close = median(btc_clean$close),
SD_Close = sd(btc_clean$close)
)
summary_stats
## Min_Close Max_Close Mean_Close Median_Close SD_Close
## 1 105724.2 124723 114879.7 115401.1 4187.243
# Compute daily returns
btc_clean$return <- c(NA, diff(log(btc_clean$close)))
# Volatility (std dev of returns)
volatility <- sd(na.omit(btc_clean$return))
volatility
## [1] 0.01559393
library(ggplot2)
# Closing price trend
ggplot(btc_clean, aes(x = date, y = close)) +
geom_line(color = "steelblue", size = 1) +
labs(
title = "Bitcoin Closing Price — Last 100 Days",
x = "Date",
y = "BTC Close (USD)"
) +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Daily returns plot
ggplot(btc_clean, aes(x = date, y = return)) +
geom_line(color = "darkred", size = 0.8) +
labs(
title = "Bitcoin Daily Log Returns",
x = "Date",
y = "Log Return"
) +
theme_minimal()
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).