For this assignment, I will use a dataset that I got originally from kaggle and uploaded to github. The dataset includes daily bitcoin historical data from 2018 to 2026. I will use window functions to calculate the year-to-date average and the six-day moving averages for each observation in the year 2026.
Next I will take a subset of the data for only items in 2026 and include only the “Close Time” and “Close” columns. These columns represent the date and closing price for the specific observation. I also extracted the date out of the “Close Time” data point because the close time was not needed since it was always 23:59 UTC time.
With the subset of observations from 2026, I’ll create another column that shows the YTD average. In order to get the year to date average, I first have to arrange the data in order by the close time and then calculate the cumulative mean on the close price.
Warning: There was 1 warning in `arrange()`.
ℹ In argument: `..1 = btc_2026_df$`Close time``.
Caused by warning:
! Unknown or uninitialised column: `Close time`.
Next I will create a column for the six day moving averages for the 2026 bitcoin data. In order to get the six day moving average, I’ll use the rollmean() function from the “zoo” package.
library(zoo) # Required for rollmean()
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
btc_2026_df <- btc_2026_df |>arrange(btc_2026_df$`Close time`) |>mutate(# Rolling (6-Day) - Window moves with the date# k=6: The window size# fill=NA: Fills the first 5 days with NA (since they don't have enough history)# align="right": Calculates using the current day + previous 5 days"6 Day Moving Average"=rollmean(btc_2026_df$Close, k =6, fill =NA, align ="right") )
Warning: There was 1 warning in `arrange()`.
ℹ In argument: `..1 = btc_2026_df$`Close time``.
Caused by warning:
! Unknown or uninitialised column: `Close time`.
head(btc_2026_df, 10)
# A tibble: 10 × 4
Date Close `YTD Average` `6 Day Moving Average`
<date> <dbl> <dbl> <dbl>
1 2026-01-01 87875. 87875. NA
2 2026-01-02 88732. 88304. NA
3 2026-01-03 90186. 88931. NA
4 2026-01-04 91104 89474. NA
5 2026-01-05 92948. 90169. NA
6 2026-01-06 93956. 90800. 90800.
7 2026-01-07 92531. 91047. 91576.
8 2026-01-08 91429. 91095. 92026.
9 2026-01-09 91179. 91105. 92191.
10 2026-01-10 90641. 91058. 92114.
Finally, to improve readability, I’ll change the column name for “Close” to “Price”, and add currency formatting for the price, YTD average, and 6 day moving average. I also removed the decimal values that represent the cent values on the prices because the numbers are large and the cent values are just insignificant in this context.
Using the table above, I am able to see that even though the price of bitcoin dipped below $70,000 in February 2026, the YTD average up to that point was still above $80,000 while the 6 day moving average was a lot closer to the actual price at around $70,000. I think in this example, the 6 day moving average is a much better average because it gives a value closer to the current price.