Cleaning & Analyzing Cryptocurrency Data Using the tidyverse in R

This page contains the code that corresponds to the following video:

 

 

The video was published on Albert Rapp’s YouTube channel and explains how to import, manipulate, and visualize a cryptocurrency data set containing Bitcoin and Ethereum values over time.

Here’s the code:

# install.packages("tidyverse")                   # Install tidyverse packages
library("tidyverse")                              # Load tidyverse packages
 
my_path <- "D:/Dropbox/Jock/Data Sets/"           # Specify directory path
 
tib_cc <- read_csv(str_c(my_path,                 # Import CSV file
                         "ETH-BTC-USD.csv"))
tib_cc                                            # Print tibble
 
tib_cc %>%                                        # Class of data set
  class()
 
tib_cc %>%                                        # Show entire data set
  View()
 
identical(tib_cc$Close, tib_cc$`Adj Close`)       # Compare two columns
 
tib_cc_sub <- tib_cc %>%                          # Extract certain columns
  select(Date, Close, Currency)
tib_cc_sub                                        # Print updated tibble
 
tib_cc_sub_date <- tib_cc_sub %>%                 # Convert character to date
  mutate(Date = mdy(Date))
tib_cc_sub_date                                   # Print updated tibble
 
tib_cc_sub_date %>%                               # Create line plot
  ggplot(aes(x = Date, 
             y = Close,
             col = Currency)) +
  geom_line()
 
tib_cc_sub_date_index <- tib_cc_sub_date %>%      # Calculate index
  group_by(Currency) %>%
  mutate(Close_index = Close / first(Close) * 100) %>%
  ungroup()
tib_cc_sub_date_index                             # Print updated tibble
 
tib_cc_sub_date_index %>%                         # Draw indexed line plot
  ggplot(aes(x = Date, 
             y = Close_index,
             col = Currency)) +
  geom_line()
 
tib_cc %>%                                        # Do all in one run
  select(Date, Close, Currency) %>% 
  mutate(Date = mdy(Date)) %>%
  group_by(Currency) %>%
  mutate(Close_index = Close / first(Close) * 100) %>%
  ungroup() %>% 
  ggplot(aes(x = Date, 
             y = Close_index,
             col = Currency)) +
  geom_line()

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top