Assignment 04

Author
Affiliation

Moisieiev Vasyl

Kyiv School of Economics

Exercise 1

library(tidyverse)
library(rvest)

url <- "https://minfin.com.ua/ua/currency/mb/archive/usd/01-02-2022/"
page <- read_html(url)

page
{html_document}
<html lang="uk">
[1] <head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8 ...
[2] <body data-platform="Windows" data-browser="unknown" data-version="" data ...

Exercise 2

table <- html_table(page)[[1]]
colnames(table)
[1] "Час"            "Курскупівлігрн" "Курспродажугрн" ""              

Exercise 3

table_tbl <- table[, 1:3] %>%
rename(
    Time = names(table)[1],
    BuyRateUAH = names(table)[2],
    SellRateUAH = names(table)[3]
  ) %>%
  filter(!is.na(Time) & Time != "")
table_tbl

Exercise 4

library(knitr)
library(kableExtra)

custom_period <- seq(ymd("2022-01-01"), ymd("2022-02-24"), by = "1 day")

get_data_for_date <- function(date) {
  url <- paste0("https://minfin.com.ua/ua/currency/mb/archive/usd/", format(date, "%d-%m-%Y"), "/")
  page <- read_html(url)
  
  tryCatch({
      page <- read_html(url)
      
      table <- html_table(page)[[1]]
      
      if (nrow(table) == 1) {
        message(paste("Table is empty for date:", date))
        return(NULL)
      }
      
      table_tbl <- table[, 1:3] %>%
        rename(
            Time = names(table)[1],
            BuyRateUAH = names(table)[2],
            SellRateUAH = names(table)[3]
          ) %>%
        filter(!is.na(Time) & Time != "") %>%
        mutate(Date = date)
      
    }, error = function(e) {
      print(paste("Error scraping data for:", date))
      scraped_data[[as.character(date)]] <- NA
    })
  return(table_tbl)
}

data_list <- lapply(custom_period, get_data_for_date)

final_data <- data_list %>%
  keep(~ !is.null(.x)) %>%
  bind_rows()

final_data %>%
  kable("html", caption = "Currency Rates (USD to UAH)", align = "c") %>%
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed"), 
    full_width = F
  ) %>%
  column_spec(1, width = "15em") %>%
  column_spec(2, width = "10em", color = "blue") %>%
  column_spec(3, width = "10em", color = "red") %>%
  add_header_above(c(" " = 1, "Rates" = 3)) %>%
  row_spec(0, bold = TRUE, font_size = 14)
Currency Rates (USD to UAH)
Rates
Time BuyRateUAH SellRateUAH Date
09:59 27.350 27.370 2022-01-04
10:00 27.350 27.370 2022-01-04
11:00 27.300 27.320 2022-01-04
12:00 27.285 27.305 2022-01-04
13:00 27.280 27.300 2022-01-04
14:00 27.280 27.300 2022-01-04
15:00 27.295 27.315 2022-01-04
16:00 27.345 27.365 2022-01-04
17:00 27.395 27.415 2022-01-04
09:59 27.420 27.440 2022-01-05
10:00 27.420 27.440 2022-01-05
11:00 27.445 27.465 2022-01-05
12:00 27.445 27.465 2022-01-05
13:00 27.440 27.460 2022-01-05
14:00 27.450 27.470 2022-01-05
15:00 27.460 27.480 2022-01-05
16:00 27.465 27.485 2022-01-05
17:00 27.455 27.475 2022-01-05
09:59 27.500 27.520 2022-01-06
10:00 27.500 27.520 2022-01-06
11:00 27.520 27.540 2022-01-06
12:00 27.485 27.505 2022-01-06
13:00 27.485 27.505 2022-01-06
14:00 27.485 27.505 2022-01-06
15:00 27.490 27.510 2022-01-06
16:00 27.475 27.495 2022-01-06
17:00 27.495 27.515 2022-01-06
09:59 27.490 27.510 2022-01-10
10:00 27.490 27.510 2022-01-10
11:00 27.515 27.535 2022-01-10
12:00 27.485 27.505 2022-01-10
13:00 27.485 27.505 2022-01-10
14:00 27.485 27.505 2022-01-10
15:00 27.490 27.510 2022-01-10
16:00 27.485 27.505 2022-01-10
17:00 27.510 27.530 2022-01-10
09:59 27.500 27.520 2022-01-11
10:00 27.500 27.520 2022-01-11
11:00 27.485 27.505 2022-01-11
12:00 27.495 27.515 2022-01-11
13:00 27.515 27.535 2022-01-11
14:00 27.515 27.535 2022-01-11
15:00 27.565 27.585 2022-01-11
16:00 27.610 27.630 2022-01-11
17:00 27.610 27.630 2022-01-11
09:59 27.650 27.670 2022-01-12
10:00 27.650 27.670 2022-01-12
11:00 27.690 27.710 2022-01-12
12:00 27.685 27.705 2022-01-12
13:00 27.725 27.745 2022-01-12
14:00 27.725 27.745 2022-01-12
15:00 27.720 27.740 2022-01-12
16:00 27.735 27.755 2022-01-12
17:00 27.730 27.750 2022-01-12
09:59 27.730 27.750 2022-01-13
10:00 27.730 27.750 2022-01-13
11:00 27.720 27.740 2022-01-13
12:00 27.715 27.735 2022-01-13
13:00 27.725 27.745 2022-01-13
14:00 27.725 27.745 2022-01-13
15:00 27.785 27.805 2022-01-13
16:00 27.830 27.850 2022-01-13
17:00 27.850 27.870 2022-01-13
09:59 27.910 27.930 2022-01-14
10:00 27.910 27.930 2022-01-14
11:00 27.925 27.945 2022-01-14
12:00 27.950 27.970 2022-01-14
13:00 27.975 27.995 2022-01-14
14:00 27.975 27.995 2022-01-14
15:00 27.970 27.990 2022-01-14
16:00 27.985 28.005 2022-01-14
17:00 27.995 28.015 2022-01-14
09:59 28.050 28.070 2022-01-17
10:00 28.050 28.070 2022-01-17
11:00 28.080 28.100 2022-01-17
12:00 28.105 28.125 2022-01-17
13:00 28.135 28.155 2022-01-17
14:00 28.145 28.165 2022-01-17
15:00 28.185 28.205 2022-01-17
16:00 28.245 28.265 2022-01-17
17:00 28.265 28.285 2022-01-17
09:59 28.360 28.380 2022-01-18
10:00 28.360 28.380 2022-01-18
11:00 28.370 28.390 2022-01-18
12:00 28.390 28.410 2022-01-18
13:00 28.460 28.480 2022-01-18
14:00 28.480 28.500 2022-01-18
15:00 28.390 28.410 2022-01-18
16:00 28.370 28.390 2022-01-18
17:00 28.370 28.390 2022-01-18
09:59 28.360 28.380 2022-01-19
10:00 28.360 28.380 2022-01-19
11:00 28.370 28.390 2022-01-19
12:00 28.430 28.450 2022-01-19
13:00 28.415 28.435 2022-01-19
14:00 28.420 28.440 2022-01-19
15:00 28.380 28.400 2022-01-19
16:00 28.330 28.350 2022-01-19
17:00 28.290 28.310 2022-01-19
09:59 28.260 28.280 2022-01-20
10:00 28.260 28.280 2022-01-20
11:00 28.285 28.305 2022-01-20
12:00 28.305 28.325 2022-01-20
13:00 28.345 28.365 2022-01-20
14:00 28.345 28.365 2022-01-20
15:00 28.340 28.360 2022-01-20
16:00 28.315 28.335 2022-01-20
17:00 28.320 28.340 2022-01-20
09:59 28.310 28.330 2022-01-21
10:00 28.310 28.330 2022-01-21
11:00 28.330 28.350 2022-01-21
12:00 28.305 28.325 2022-01-21
13:00 28.305 28.325 2022-01-21
14:00 28.305 28.325 2022-01-21
15:00 28.280 28.300 2022-01-21
16:00 28.220 28.240 2022-01-21
17:00 28.215 28.235 2022-01-21
09:59 28.290 28.310 2022-01-24
10:00 28.290 28.310 2022-01-24
11:00 28.320 28.340 2022-01-24
12:00 28.355 28.375 2022-01-24
13:00 28.380 28.400 2022-01-24
14:00 28.380 28.400 2022-01-24
15:00 28.430 28.450 2022-01-24
16:00 28.520 28.540 2022-01-24
17:00 28.580 28.600 2022-01-24
09:59 28.620 28.640 2022-01-25
10:00 28.620 28.640 2022-01-25
11:00 28.630 28.650 2022-01-25
12:00 28.655 28.675 2022-01-25
13:00 28.650 28.670 2022-01-25
14:00 28.660 28.680 2022-01-25
15:00 28.700 28.720 2022-01-25
16:00 28.720 28.740 2022-01-25
17:00 28.710 28.730 2022-01-25
09:59 28.720 28.740 2022-01-26
10:00 28.720 28.740 2022-01-26
11:00 28.770 28.790 2022-01-26
12:00 28.795 28.815 2022-01-26
13:00 28.775 28.795 2022-01-26
14:00 28.775 28.795 2022-01-26
15:00 28.800 28.820 2022-01-26
16:00 28.805 28.825 2022-01-26
17:00 28.805 28.825 2022-01-26
09:59 28.805 28.825 2022-01-27
10:00 28.805 28.825 2022-01-27
11:00 28.950 28.970 2022-01-27
12:00 28.970 28.990 2022-01-27
13:00 29.000 29.020 2022-01-27
14:00 29.000 29.020 2022-01-27
15:00 29.010 29.030 2022-01-27
16:00 28.950 28.970 2022-01-27
17:00 28.900 28.920 2022-01-27
09:59 28.850 28.870 2022-01-28
10:00 28.850 28.870 2022-01-28
11:00 28.820 28.840 2022-01-28
12:00 28.785 28.805 2022-01-28
13:00 28.690 28.710 2022-01-28
14:00 28.670 28.690 2022-01-28
15:00 28.650 28.670 2022-01-28
16:00 28.670 28.690 2022-01-28
16:00 28.670 28.690 2022-01-28
17:00 28.680 28.700 2022-01-28
09:59 28.570 28.590 2022-01-31
10:00 28.570 28.590 2022-01-31
11:00 28.555 28.575 2022-01-31
12:00 28.435 28.455 2022-01-31
13:00 28.410 28.430 2022-01-31
14:00 28.410 28.430 2022-01-31
15:00 28.415 28.435 2022-01-31
16:00 28.405 28.425 2022-01-31
17:00 28.370 28.390 2022-01-31
09:59 28.270 28.290 2022-02-01
10:00 28.270 28.290 2022-02-01
11:00 28.300 28.320 2022-02-01
12:00 28.315 28.335 2022-02-01
13:00 28.420 28.440 2022-02-01
14:00 28.430 28.450 2022-02-01
15:00 28.390 28.410 2022-02-01
16:00 28.390 28.410 2022-02-01
17:00 28.410 28.430 2022-02-01
09:59 28.400 28.420 2022-02-02
10:00 28.400 28.420 2022-02-02
11:00 28.420 28.440 2022-02-02
12:00 28.385 28.405 2022-02-02
13:00 28.380 28.400 2022-02-02
14:00 28.380 28.400 2022-02-02
15:00 28.330 28.350 2022-02-02
16:00 28.290 28.310 2022-02-02
17:00 28.285 28.305 2022-02-02
09:59 28.290 28.310 2022-02-03
10:00 28.290 28.310 2022-02-03
11:00 28.260 28.280 2022-02-03
12:00 28.260 28.280 2022-02-03
13:00 28.265 28.285 2022-02-03
14:00 28.265 28.285 2022-02-03
15:00 28.255 28.275 2022-02-03
16:00 28.250 28.270 2022-02-03
17:00 28.240 28.260 2022-02-03
09:59 28.220 28.240 2022-02-04
10:00 28.220 28.240 2022-02-04
11:00 28.195 28.215 2022-02-04
12:00 28.115 28.135 2022-02-04
13:00 28.115 28.135 2022-02-04
14:00 28.115 28.135 2022-02-04
15:00 28.105 28.125 2022-02-04
16:00 28.100 28.120 2022-02-04
17:00 28.115 28.135 2022-02-04
09:59 28.080 28.100 2022-02-07
10:00 28.080 28.100 2022-02-07
11:00 28.005 28.025 2022-02-07
12:00 27.990 28.010 2022-02-07
13:00 27.985 28.005 2022-02-07
14:00 27.985 28.005 2022-02-07
15:00 27.965 27.985 2022-02-07
16:00 27.970 27.990 2022-02-07
17:00 27.940 27.960 2022-02-07
09:59 27.920 27.940 2022-02-08
10:00 27.920 27.940 2022-02-08
11:00 27.950 27.970 2022-02-08
12:00 27.985 28.005 2022-02-08
13:00 28.000 28.020 2022-02-08
14:00 27.990 28.010 2022-02-08
15:00 27.965 27.985 2022-02-08
16:00 27.980 28.000 2022-02-08
17:00 27.985 28.005 2022-02-08
09:59 27.980 28.000 2022-02-09
10:00 27.980 28.000 2022-02-09
11:00 27.985 28.005 2022-02-09
12:00 27.980 28.000 2022-02-09
13:00 27.970 27.990 2022-02-09
14:00 27.955 27.975 2022-02-09
15:00 27.935 27.955 2022-02-09
16:00 27.925 27.945 2022-02-09
17:00 27.925 27.945 2022-02-09
09:59 27.900 27.920 2022-02-10
10:00 27.900 27.920 2022-02-10
11:00 27.885 27.905 2022-02-10
12:00 27.865 27.885 2022-02-10
13:00 27.835 27.855 2022-02-10
14:00 27.840 27.860 2022-02-10
15:00 27.835 27.855 2022-02-10
16:00 27.840 27.860 2022-02-10
17:00 27.890 27.910 2022-02-10
09:59 27.940 27.960 2022-02-11
10:00 27.940 27.960 2022-02-11
11:00 28.030 28.050 2022-02-11
12:00 28.050 28.070 2022-02-11
13:00 28.035 28.055 2022-02-11
14:00 28.035 28.055 2022-02-11
15:00 27.995 28.015 2022-02-11
16:00 28.020 28.040 2022-02-11
17:00 28.040 28.060 2022-02-11
09:59 28.700 28.720 2022-02-14
10:00 28.700 28.720 2022-02-14
11:00 28.470 28.520 2022-02-14
12:00 28.520 28.560 2022-02-14
13:00 28.550 28.570 2022-02-14
14:00 28.530 28.550 2022-02-14
15:00 28.510 28.530 2022-02-14
16:00 28.460 28.480 2022-02-14
17:00 28.495 28.515 2022-02-14
09:59 28.480 28.500 2022-02-15
10:00 28.480 28.500 2022-02-15
11:00 28.220 28.240 2022-02-15
12:00 28.175 28.195 2022-02-15
13:00 28.205 28.225 2022-02-15
14:00 28.205 28.225 2022-02-15
15:00 28.190 28.210 2022-02-15
16:00 28.190 28.210 2022-02-15
17:00 28.210 28.230 2022-02-15
09:59 28.080 28.100 2022-02-16
10:00 28.080 28.100 2022-02-16
11:00 28.070 28.090 2022-02-16
12:00 28.095 28.115 2022-02-16
13:00 28.100 28.120 2022-02-16
13:00 28.100 28.120 2022-02-16
14:00 28.100 28.120 2022-02-16
15:00 28.090 28.110 2022-02-16
16:00 28.090 28.110 2022-02-16
17:00 28.150 28.170 2022-02-16
09:59 28.190 28.210 2022-02-17
10:00 28.190 28.210 2022-02-17
11:00 28.265 28.285 2022-02-17
12:00 28.335 28.355 2022-02-17
13:00 28.305 28.325 2022-02-17
14:00 28.305 28.325 2022-02-17
15:00 28.310 28.330 2022-02-17
16:00 28.350 28.370 2022-02-17
17:00 28.370 28.390 2022-02-17
09:59 28.320 28.340 2022-02-18
10:00 28.320 28.340 2022-02-18
11:00 28.345 28.365 2022-02-18
12:00 28.340 28.360 2022-02-18
13:00 28.325 28.345 2022-02-18
14:00 28.325 28.345 2022-02-18
15:00 28.345 28.365 2022-02-18
16:00 28.365 28.385 2022-02-18
17:00 28.415 28.435 2022-02-18
09:59 28.400 28.420 2022-02-21
10:00 28.400 28.420 2022-02-21
11:00 28.470 28.490 2022-02-21
12:00 28.475 28.485 2022-02-21
13:00 28.455 28.475 2022-02-21
14:00 28.465 28.485 2022-02-21
15:00 28.520 28.540 2022-02-21
16:00 28.570 28.590 2022-02-21
17:00 28.595 28.615 2022-02-21
09:59 29.050 29.070 2022-02-22
10:00 29.050 29.070 2022-02-22
11:00 29.000 29.020 2022-02-22
12:00 28.950 28.970 2022-02-22
13:00 28.945 28.965 2022-02-22
14:00 28.945 28.965 2022-02-22
15:00 29.020 29.040 2022-02-22
16:00 29.050 29.070 2022-02-22
17:00 29.040 29.060 2022-02-22
09:59 29.080 29.100 2022-02-23
10:00 29.080 29.100 2022-02-23
11:00 29.120 29.140 2022-02-23
12:00 29.140 29.160 2022-02-23
13:00 29.310 29.350 2022-02-23
14:00 29.310 29.350 2022-02-23
15:00 29.550 29.600 2022-02-23
16:00 29.440 29.490 2022-02-23
17:00 29.430 29.480 2022-02-23
09:59 29.800 29.950 2022-02-24
10:00 29.800 29.950 2022-02-24

Exercise 5

library(ggplot2)

ggplot(final_data, aes(x = Date)) +
  geom_line(aes(y = BuyRateUAH, color = "BuyRateUAH"), size = 1) +
  geom_line(aes(y = SellRateUAH, color = "SellRateUAH"), size = 1) +
  labs(
    title = "Change of Buy and Sell Rates Over Time",
    x = "Date",
    y = "Exchange Rate (UAH)",
    color = "Rates"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))