Section - 4 Data Prep

Next we will do some data cleaning to make sure our data is in the format we need it to be in. For a gentler introduction to data prep using the dplyr package (Wickham, François, et al. 2020) consult the high-level version.

4.1 Remove Nulls

First off, we aren’t able to do anything at all with a row of data if we don’t know when the data was collected. The specific price doesn’t matter if we can’t tie it to a timestamp, given by the date_time_utc field.

We can exclude all rows where the date_time_utc field has a Null (missing) value by using the filter() function from the dplyr package:

cryptodata <- filter(cryptodata, !is.na(date_time_utc))

This step removed 0 rows from the data on the latest run (2021-12-04). The is.na() function finds all cases where the date_time_utc field has a Null value. The function is preceded by the ! operator, which tells the filter() function to exclude these rows from the data.

By the same logic, if we don’t know what the price was for any of the rows, the whole row of data is useless and should be removed. But how will we define the price of a cryptocurrency?

4.2 Calculate price_usd Column

In the previous section we discussed the intricacies of a cryptocurrency’s price. We could complicate our definition of a price by considering both the bid and ask prices from the perspective of someone who wants to perform trades, but this is not a trading tutorial. Instead, we will define the price of a cryptocurrency as the price we could purchase it for. We will calculate the price_usd field using the cheapest price available from the ask side where at least $15 worth of the cryptocurrency are being sold.

Therefore, let’s figure out the lowest price from the order book data that would allow us to purchase at least $15 worth of the cryptocurrency. To do this, for each ask price and quantity, let’s figure out the value of the trade in US Dollars. We can create each of the new trade_usd columns using the mutate() function. The trade_usd_1 should be calculated as the ask_1_price multiplied by the ask_1_quantity. The next one trade_usd_2 should consider the ask_2_price, but be multiplied by the sum of ask_1_quantity and ask_2_quantity because at the ask_2_price pricepoint we can also purchase the quantity available at the ask_1_price pricepoint:

cryptodata <- mutate(cryptodata, 
                     trade_usd_1 = ask_1_price * ask_1_quantity,
                     trade_usd_2 = ask_2_price * (ask_1_quantity + ask_2_quantity),
                     trade_usd_3 = ask_3_price * (ask_1_quantity + ask_2_quantity + ask_3_quantity),
                     trade_usd_4 = ask_4_price * (ask_1_quantity + ask_2_quantity + ask_3_quantity + ask_4_quantity),
                     trade_usd_5 = ask_5_price * (ask_1_quantity + ask_2_quantity + ask_3_quantity + ask_4_quantity + ask_5_quantity))

We can confirm that the trade_usd_1 field is calculating the $ value of the lowest ask price and quantity:

head(select(cryptodata, symbol, date_time_utc, ask_1_price, ask_1_quantity, trade_usd_1))
## # A tibble: 6 x 5
##   symbol date_time_utc       ask_1_price ask_1_quantity trade_usd_1
##   <chr>  <dttm>                    <dbl>          <dbl>       <dbl>
## 1 BTC    2021-12-04 00:00:01    53649.            0.335    17972.  
## 2 ETH    2021-12-04 00:00:01     4217.            3.82     16111.  
## 3 EOS    2021-12-04 00:00:02        3.79         13.5         51.1 
## 4 LTC    2021-12-04 00:00:03      188.            1.95       367.  
## 5 BSV    2021-12-04 00:00:08      141.            0.01         1.41
## 6 ADA    2021-12-04 00:00:09        1.56        118          184.

Now we can use the mutate() function to create the new field price_usd and find the lowest price at which we could have purchased at least $15 worth of the cryptocurrency. We can use the case_when() function to find the first trade_usd value that is greater or equal to $15, and assign the correct ask_price for the new column price_usd:

cryptodata <- mutate(cryptodata, 
                     price_usd = case_when(
                       cryptodata$trade_usd_1 >= 15 ~ cryptodata$ask_1_price,
                       cryptodata$trade_usd_2 >= 15 ~ cryptodata$ask_2_price,
                       cryptodata$trade_usd_3 >= 15 ~ cryptodata$ask_3_price,
                       cryptodata$trade_usd_4 >= 15 ~ cryptodata$ask_4_price,
                       cryptodata$trade_usd_5 >= 15 ~ cryptodata$ask_5_price))

Let’s also remove any rows that have Null values for the new price_usd field like we did for the date_time_utc field in a previous step. These will mostly be made up of rows where the value of trades through the 5th lowest ask price was lower than $15.

cryptodata <- filter(cryptodata, !is.na(price_usd))

This step removed 16940 rows on the latest run.

4.3 Clean Data by Group

In the high-level version of this tutorial we only dealt with one cryptocurrency. In this version however, we will be creating independent models for each cryptocurrency. Because of this, we need to ensure data quality not only for the data as a whole, but also for the data associated with each individual cryptocurrency. Instead of considering all rows when applying a transformation, we can group the data by the individual cryptocurrency and apply the transformation to each group. This will only work with compatible functions from dplyr and the tidyverse.

For example, we could count the number of observations by running the count() function on the data:

count(cryptodata)
## # A tibble: 1 x 1
##        n
##    <int>
## 1 283060

But what if we wanted to know how many observations in the data are associated with each cryptocurrency separately?

We can group the data using the group_by() function from the dplyr package and group the data by the cryptocurrency symbol:

cryptodata <- group_by(cryptodata, symbol)

Now if we run the same operation using the count() function, the operation is performed grouped by the cryptocurrency symbol:

count(cryptodata)
## # A tibble: 181 x 2
## # Groups:   symbol [181]
##    symbol     n
##    <chr>  <int>
##  1 1INCH   1828
##  2 ACH     1827
##  3 ACT     1545
##  4 ADA     1829
##  5 ALPHA   1825
##  6 APPC    1483
##  7 AR       704
##  8 ARDR    1829
##  9 ARK     1531
## 10 ARPA    1828
## # ... with 171 more rows

We can remove the grouping at any point by running the ungroup() function:

count(ungroup(cryptodata))
## # A tibble: 1 x 1
##        n
##    <int>
## 1 283060

4.3.1 Remove symbols without enough rows

Because this dataset evolves over time, we will need to be proactive about issues that may arise even if they aren’t currently a problem.

What happens if a new cryptocurrency gets added to the cryptocurrency exchange? If we only had a couple days of data for an asset, not only would that not be enough information to build effective predictive models, but we may run into actual errors since the data will be further split into more groups to validate the results of the models against several datasets using cross validation, more to come on that later.

To ensure we have a reasonable amount of data for each individual cryptocurrency, let’s filter out any cryptocurrencies that don’t have at least 1,000 observations using the filter() function:

cryptodata <- filter(cryptodata, n() >= 1000)

The number of rows for the cryptodata dataset before the filtering step was 283060 and is now 275334. This step removed 20 cryptocurrencies from the analysis that did not have enough observations associated with them.

4.3.2 Remove symbols without data from the last 3 days

If there was no data collected for a cryptocurrency over the last 3 day period, let’s exclude that asset from the dataset since we are only looking to model data that is currently flowing through the process. If an asset is removed from the exchange (if a project is a scam for example) or is no longer being actively captured by the data collection process, we can’t make new predictions for it, so might as well exclude these ahead of time as well.

cryptodata <- filter(cryptodata, max(date) > Sys.Date()-3)

The number of rows for the cryptodata dataset before this filtering step was 262098 and is now 275334.

4.4 Calculate Target

Our goal is to be able to make predictions on the price of each cryptocurrency 24 hours into the future from when the data was collected. Therefore, the target variable that we will be using as what we want to predict for the predictive models, is the price 24 hours into the future relative to when the data was collected.

To do this we can create a new column in the data that is the price_usd offset by 24 rows (one for each hour), but before we can do that we need to make sure there are no gaps anywhere in the data.

4.4.1 Convert to tsibble

We can fill any gaps in the data using the tsibble package (Wang et al. 2020), which was covered in more detail in the high-level version of the tutorial.

4.4.1.1 Convert to hourly data

The data we are using was collected between the 0th and the 5th minute of every hour; it is collected in the same order every hour to try and get the timing as consistent as possible for each cryptocurrency, but the cadence is not exactly one hour. Therefore, if we convert the data now to a tsibble object, it would recognize the data as being collected on the wrong cadence.

To fix this issue, let’s create a new column called ts_index using the mutate() function which will keep the information relating to the date and hour collected, but generalize the minutes and seconds as “00:00”, which will be correctly recognized by the tsibble package as being data collected on an hourly basis. The pkDummy field contains the date and hour, so we can add the text “:00:00” to the end of that field, and then convert the new string to a date time object using the anytime() function from the anytime package (Eddelbuettel 2020):

cryptodata <- mutate(cryptodata, ts_index = anytime(paste0(pkDummy,':00:00')))

Before we can convert the data to be a tsibble and easily fill in the gaps, we also need to make sure there are no duplicate values in the ts_index column for each cryptocurrency. There shouldn’t be any duplicates, but just in case any make their way into the data somehow, we can use the distinct() function from the dplyr package to prevent the issue from potentially arising:

cryptodata <- distinct(cryptodata, symbol, ts_index, .keep_all=TRUE)

Now we can finally convert the table to a tsibble data type by using the as_tsibble() function from the tsibble package (Wang et al. 2020), and providing the symbol column for the key parameter to preserve the grouped structure:

cryptodata <- as_tsibble(cryptodata, index = ts_index, key = symbol)

Notice how the preview of the data below looks a bit different from the summary we were seeing up to this point, and now it says “A tsibble”, and next to the table dimensions says [1h], indicating the observations are 1 hour apart from each other. The second row tells us the “Key” of the tsibble is the symbol column

cryptodata
## # A tsibble: 258,925 x 34 [1h] <UTC>
## # Key:       symbol [151]
## # Groups:    symbol [151]
##    pair  symbol quote_currency ask_1_price ask_1_quantity ask_2_price
##    <chr> <chr>  <chr>                <dbl>          <dbl>       <dbl>
##  1 1INC~ 1INCH  USD                   3.14           151.        3.14
##  2 1INC~ 1INCH  USD                   3.07           151.        3.07
##  3 1INC~ 1INCH  USD                   3.10           152.        3.10
##  4 1INC~ 1INCH  USD                   3.11           151.        3.11
##  5 1INC~ 1INCH  USD                   3.09           151.        3.09
##  6 1INC~ 1INCH  USD                   3.08           167.        3.08
##  7 1INC~ 1INCH  USD                   3.12           167.        3.12
##  8 1INC~ 1INCH  USD                   3.13           167.        3.13
##  9 1INC~ 1INCH  USD                   3.14           159.        3.14
## 10 1INC~ 1INCH  USD                   3.12           159.        3.12
## # ... with 258,915 more rows, and 28 more variables: ask_2_quantity <dbl>,
## #   ask_3_price <dbl>, ask_3_quantity <dbl>, ask_4_price <dbl>,
## #   ask_4_quantity <dbl>, ask_5_price <dbl>, ask_5_quantity <dbl>,
## #   bid_1_price <dbl>, bid_1_quantity <dbl>, bid_2_price <dbl>,
## #   bid_2_quantity <dbl>, bid_3_price <dbl>, bid_3_quantity <dbl>,
## #   bid_4_price <dbl>, bid_4_quantity <dbl>, bid_5_price <dbl>,
## #   bid_5_quantity <dbl>, date_time_utc <dttm>, date <date>, pkDummy <chr>,
## #   pkey <chr>, trade_usd_1 <dbl>, trade_usd_2 <dbl>, trade_usd_3 <dbl>,
## #   trade_usd_4 <dbl>, trade_usd_5 <dbl>, price_usd <dbl>, ts_index <dttm>

4.4.2 Fill gaps

Now we can use the fill_gaps() function from the tsibble package to fill any gaps found in the data, as being implicitly Null. Meaning, we will add these rows into the data with NA values for everything except for the date time field. This will allow us to safely compute the target price found 24 hours into the future relative to when each row was collected.

cryptodata <- fill_gaps(cryptodata)

Now looking at the data again, there are 15364 additional rows that were added as implicitly missing in the data:

cryptodata
## # A tsibble: 274,289 x 34 [1h] <UTC>
## # Key:       symbol [151]
## # Groups:    symbol [151]
##    pair  symbol quote_currency ask_1_price ask_1_quantity ask_2_price
##    <chr> <chr>  <chr>                <dbl>          <dbl>       <dbl>
##  1 1INC~ 1INCH  USD                   3.14           151.        3.14
##  2 1INC~ 1INCH  USD                   3.07           151.        3.07
##  3 1INC~ 1INCH  USD                   3.10           152.        3.10
##  4 1INC~ 1INCH  USD                   3.11           151.        3.11
##  5 1INC~ 1INCH  USD                   3.09           151.        3.09
##  6 1INC~ 1INCH  USD                   3.08           167.        3.08
##  7 1INC~ 1INCH  USD                   3.12           167.        3.12
##  8 1INC~ 1INCH  USD                   3.13           167.        3.13
##  9 1INC~ 1INCH  USD                   3.14           159.        3.14
## 10 1INC~ 1INCH  USD                   3.12           159.        3.12
## # ... with 274,279 more rows, and 28 more variables: ask_2_quantity <dbl>,
## #   ask_3_price <dbl>, ask_3_quantity <dbl>, ask_4_price <dbl>,
## #   ask_4_quantity <dbl>, ask_5_price <dbl>, ask_5_quantity <dbl>,
## #   bid_1_price <dbl>, bid_1_quantity <dbl>, bid_2_price <dbl>,
## #   bid_2_quantity <dbl>, bid_3_price <dbl>, bid_3_quantity <dbl>,
## #   bid_4_price <dbl>, bid_4_quantity <dbl>, bid_5_price <dbl>,
## #   bid_5_quantity <dbl>, date_time_utc <dttm>, date <date>, pkDummy <chr>,
## #   pkey <chr>, trade_usd_1 <dbl>, trade_usd_2 <dbl>, trade_usd_3 <dbl>,
## #   trade_usd_4 <dbl>, trade_usd_5 <dbl>, price_usd <dbl>, ts_index <dttm>

Now that all of the gaps have been filled in, let’s convert the data back to be in the structure of a tibble, which is the data structure that supports the grouping structure we discussed previously, and let’s group the data by the symbol again:

cryptodata <- group_by(as_tibble(cryptodata), symbol)

4.4.3 Calculate Target

Now we finally have everything we need to calculate the target variable containing the price 24 hours into the future relative to when the data was collected. We can use the usual mutate() function to add a new column to the data called target_price_24h, and use the lead() function from dplyr to offset the price_usd column by 24 hours:

cryptodata <- mutate(cryptodata, 
                     target_price_24h = lead(price_usd, 24, order_by=ts_index))

4.4.4 Calculate Lagged Prices

What about doing the opposite? If we added a new column showing the price from 24 hours earlier, could the price movement between then and when the data was collected help us predict where the price is headed next? If the price has gone down significantly over the previous 24 hours, is the price for the next 24 hours more likely to increase or decrease? What if the price has gone down significantly over the previous 24 hours, but has increased significantly since the past hour?

These relationships around the sensitivity of a price to recent price changes may help our models come up with more accurate forecasts about the future, so let’s go ahead and add some lagged prices using the same methodology used to calculate the target variable, but this time using the lag() function to get past observations instead of the lead() function used before:

cryptodata <- mutate(cryptodata,
                     lagged_price_1h  = lag(price_usd, 1, order_by=ts_index),
                     lagged_price_2h  = lag(price_usd, 2, order_by=ts_index),
                     lagged_price_3h  = lag(price_usd, 3, order_by=ts_index),
                     lagged_price_6h  = lag(price_usd, 6, order_by=ts_index),
                     lagged_price_12h = lag(price_usd, 12, order_by=ts_index),
                     lagged_price_24h = lag(price_usd, 24, order_by=ts_index),
                     lagged_price_3d  = lag(price_usd, 24*3, order_by=ts_index))

This step can be thought of as data engineering more than data cleaning, because rather than fixing an issue we are enhancing the dataset with columns that may help with the forecasts.

Let’s view an example of the oldest 30 rows of data associated with the Bitcoin cryptocurrency (symbol == "BTC"). With the oldest data starting from the top, the lagged_price_1h field should have a NA value for the first row because we don’t have any prices before that point. By that same logic, the lagged_price_24h column should be missing the first 24 values and have the last 6 values showing the first 6 rows of the price_usd column. The target_price_24h would values for the oldest data because the opposite is true and we don’t know the values for data for the most recent 24 rows of the data:

print(select(filter(cryptodata, symbol == 'BTC'), 
             ts_index, price_usd, lagged_price_1h, 
             lagged_price_24h, target_price_24h), n=30)
## # A tibble: 1,837 x 6
## # Groups:   symbol [1]
##    symbol ts_index            price_usd lagged_price_1h lagged_price_24h
##    <chr>  <dttm>                  <dbl>           <dbl>            <dbl>
##  1 BTC    2021-09-19 00:00:00    48303.             NA               NA 
##  2 BTC    2021-09-19 01:00:00    47931.          48303.              NA 
##  3 BTC    2021-09-19 02:00:00    47874.          47931.              NA 
##  4 BTC    2021-09-19 03:00:00    48007.          47874.              NA 
##  5 BTC    2021-09-19 04:00:00    47836.          48007.              NA 
##  6 BTC    2021-09-19 05:00:00    47845.          47836.              NA 
##  7 BTC    2021-09-19 06:00:00    47997.          47845.              NA 
##  8 BTC    2021-09-19 07:00:00    48142.          47997.              NA 
##  9 BTC    2021-09-19 08:00:00    48168.          48142.              NA 
## 10 BTC    2021-09-19 09:00:00    48253.          48168.              NA 
## 11 BTC    2021-09-19 10:00:00    48116.          48253.              NA 
## 12 BTC    2021-09-19 11:00:00    48074.          48116.              NA 
## 13 BTC    2021-09-19 12:00:00    47338.          48074.              NA 
## 14 BTC    2021-09-19 13:00:00    47279.          47338.              NA 
## 15 BTC    2021-09-19 14:00:00    47325.          47279.              NA 
## 16 BTC    2021-09-19 15:00:00    47576.          47325.              NA 
## 17 BTC    2021-09-19 16:00:00    47655.          47576.              NA 
## 18 BTC    2021-09-19 17:00:00    47537.          47655.              NA 
## 19 BTC    2021-09-19 18:00:00    47372.          47537.              NA 
## 20 BTC    2021-09-19 19:00:00    47676.          47372.              NA 
## 21 BTC    2021-09-19 20:00:00       NA           47676.              NA 
## 22 BTC    2021-09-19 21:00:00       NA              NA               NA 
## 23 BTC    2021-09-19 22:00:00       NA              NA               NA 
## 24 BTC    2021-09-19 23:00:00       NA              NA               NA 
## 25 BTC    2021-09-20 00:00:00    47261.             NA            48303.
## 26 BTC    2021-09-20 01:00:00    46994.          47261.           47931.
## 27 BTC    2021-09-20 02:00:00    47215.          46994.           47874.
## 28 BTC    2021-09-20 03:00:00    45960.          47215.           48007.
## 29 BTC    2021-09-20 04:00:00    45566.          45960.           47836.
## 30 BTC    2021-09-20 05:00:00    45655.          45566.           47845.
## # ... with 1,807 more rows, and 1 more variable: target_price_24h <dbl>

We can wrap the code used above in the tail() function to show the most recent data and see the opposite dynamic with the new fields we created:

print(tail(select(filter(cryptodata, symbol == 'BTC'), 
                  ts_index, price_usd, lagged_price_24h, 
                  target_price_24h),30), n=30)
## # A tibble: 30 x 5
## # Groups:   symbol [1]
##    symbol ts_index            price_usd lagged_price_24h target_price_24h
##    <chr>  <dttm>                  <dbl>            <dbl>            <dbl>
##  1 BTC    2021-12-03 07:00:00    56858.           56677.           47509.
##  2 BTC    2021-12-03 08:00:00    56769.           56885.           47225.
##  3 BTC    2021-12-03 09:00:00    56596.           56979.           47492.
##  4 BTC    2021-12-03 10:00:00    56828.           56707.           47488.
##  5 BTC    2021-12-03 11:00:00    56851.           56572.           47310.
##  6 BTC    2021-12-03 12:00:00    56994.           56328.           46493.
##  7 BTC    2021-12-03 13:00:00    56812.           56547.              NA 
##  8 BTC    2021-12-03 14:00:00    56881.           56485.              NA 
##  9 BTC    2021-12-03 15:00:00    56239.           57129.              NA 
## 10 BTC    2021-12-03 16:00:00    55985.           56537.              NA 
## 11 BTC    2021-12-03 17:00:00    55008.           56353.              NA 
## 12 BTC    2021-12-03 18:00:00    54990.           56609.              NA 
## 13 BTC    2021-12-03 19:00:00    54897.           56737.              NA 
## 14 BTC    2021-12-03 20:00:00    53857.           56632.              NA 
## 15 BTC    2021-12-03 21:00:00    53479.           57021.              NA 
## 16 BTC    2021-12-03 22:00:00    53640.           56880.              NA 
## 17 BTC    2021-12-03 23:00:00    53429.           56869.              NA 
## 18 BTC    2021-12-04 00:00:00    53649.           56495.              NA 
## 19 BTC    2021-12-04 01:00:00    53065.           56531.              NA 
## 20 BTC    2021-12-04 02:00:00    53000.           56518.              NA 
## 21 BTC    2021-12-04 03:00:00    53137.           56273.              NA 
## 22 BTC    2021-12-04 04:00:00    52040.           56336.              NA 
## 23 BTC    2021-12-04 05:00:00    50299            56606.              NA 
## 24 BTC    2021-12-04 06:00:00    47584.           56757.              NA 
## 25 BTC    2021-12-04 07:00:00    47509.           56858.              NA 
## 26 BTC    2021-12-04 08:00:00    47225.           56769.              NA 
## 27 BTC    2021-12-04 09:00:00    47492.           56596.              NA 
## 28 BTC    2021-12-04 10:00:00    47488.           56828.              NA 
## 29 BTC    2021-12-04 11:00:00    47310.           56851.              NA 
## 30 BTC    2021-12-04 12:00:00    46493.           56994.              NA

Reading the code shown above is less than ideal. One of the more popular tools introduced by the tidyverse is the %>% operator, which works by starting with the object/data you want to make changes to first, and then apply each transformation step by step. It’s simply a way of re-writing the same code in a way that is easier to read by splitting the way the function is called rather than adding functions onto each other into a single line that becomes really hard to read. In the example above it becomes difficult to keep track of where things begin, the order of operations, and the parameters associated with the specific functions. Compare that to the code below:

# Start with the object/data to manipulate
cryptodata %>% 
  # Filter the data to only the BTC symbol
  filter(symbol == 'BTC') %>% 
  # Select columns to display
  select(ts_index, price_usd, lagged_price_24h, target_price_24h) %>% 
  # Show the last 30 elements of the data
  tail(30) %>% 
  # Show all 30 elements instead of the default 10 for a tibble dataframe
  print(n = 30)
## # A tibble: 30 x 5
## # Groups:   symbol [1]
##    symbol ts_index            price_usd lagged_price_24h target_price_24h
##    <chr>  <dttm>                  <dbl>            <dbl>            <dbl>
##  1 BTC    2021-12-03 07:00:00    56858.           56677.           47509.
##  2 BTC    2021-12-03 08:00:00    56769.           56885.           47225.
##  3 BTC    2021-12-03 09:00:00    56596.           56979.           47492.
##  4 BTC    2021-12-03 10:00:00    56828.           56707.           47488.
##  5 BTC    2021-12-03 11:00:00    56851.           56572.           47310.
##  6 BTC    2021-12-03 12:00:00    56994.           56328.           46493.
##  7 BTC    2021-12-03 13:00:00    56812.           56547.              NA 
##  8 BTC    2021-12-03 14:00:00    56881.           56485.              NA 
##  9 BTC    2021-12-03 15:00:00    56239.           57129.              NA 
## 10 BTC    2021-12-03 16:00:00    55985.           56537.              NA 
## 11 BTC    2021-12-03 17:00:00    55008.           56353.              NA 
## 12 BTC    2021-12-03 18:00:00    54990.           56609.              NA 
## 13 BTC    2021-12-03 19:00:00    54897.           56737.              NA 
## 14 BTC    2021-12-03 20:00:00    53857.           56632.              NA 
## 15 BTC    2021-12-03 21:00:00    53479.           57021.              NA 
## 16 BTC    2021-12-03 22:00:00    53640.           56880.              NA 
## 17 BTC    2021-12-03 23:00:00    53429.           56869.              NA 
## 18 BTC    2021-12-04 00:00:00    53649.           56495.              NA 
## 19 BTC    2021-12-04 01:00:00    53065.           56531.              NA 
## 20 BTC    2021-12-04 02:00:00    53000.           56518.              NA 
## 21 BTC    2021-12-04 03:00:00    53137.           56273.              NA 
## 22 BTC    2021-12-04 04:00:00    52040.           56336.              NA 
## 23 BTC    2021-12-04 05:00:00    50299            56606.              NA 
## 24 BTC    2021-12-04 06:00:00    47584.           56757.              NA 
## 25 BTC    2021-12-04 07:00:00    47509.           56858.              NA 
## 26 BTC    2021-12-04 08:00:00    47225.           56769.              NA 
## 27 BTC    2021-12-04 09:00:00    47492.           56596.              NA 
## 28 BTC    2021-12-04 10:00:00    47488.           56828.              NA 
## 29 BTC    2021-12-04 11:00:00    47310.           56851.              NA 
## 30 BTC    2021-12-04 12:00:00    46493.           56994.              NA

There are several advantages to writing code the tidy way, but while some love it others hate it, so we won’t force anyone to have to understand how the %>% operator works and we have stayed away from its use for the rest of the code shown, but we do encourage the use of this tool: https://magrittr.tidyverse.org/reference/pipe.html

4.5 Remove Nulls

We can’t do anything with a row of data if we don’t know when the data was collected, so let’s just double confirm that all rows have a value for the date_time_utc field by using the filter() function from the dplyr package to exclude any rows with NA values for the column:

# Remove all NA values of date_time_utc:
cryptodata <- filter(cryptodata, !is.na(date_time_utc))

This step removed 15364 rows from the data. This step mainly helps us avoid issues when programmatically labeling charts in the next section, move on to the next section ➡️ to learn some amazingly powerful tools to visualize data!

References

Eddelbuettel, Dirk. 2020. Anytime: Anything to Posixct or Date Converter. http://dirk.eddelbuettel.com/code/anytime.html.

Wang, Earo, Di Cook, Rob Hyndman, and Mitchell O’Hara-Wild. 2020. Tsibble: Tidy Temporal Data Frames and Tools. https://tsibble.tidyverts.org.

Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2020. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.