phenance.com
Stock Data Pages | TCA League Tables | About

Analyzing Bitcoin in R - Part 3 - Setting and using dates with xts

Pub: 04.12.17

| By Anonymous

In Finance.

Tags: coding r bitcoin analysis .

A function helps us to streamline the code, it takes a value in seconds and changes it to a data and time since the beginning of the epoch that is human readable and can be used by xts:

sec_to_date = function(sec) {
    as.POSIXct(sec, origin = '1970-01-01', tz = 'UTC')
}

I create a new column which contains the second time stamp converted to a date and time value.

Sys.setenv(TZ = "UTC")
rawdata$datetime <- sec_to_date(rawdata[,1])

I use the new date and time column to order the other two columns (USD price of Bitcoin and the Volume in Bitcoin).

library(xts)
rawdata <- xts(x = rawdata[,2:3], order.by = rawdata$datetime, tz = "UTC")

The data is now in a format that allows us to easily select rows by date and time and to process it further.

This shows all the rows between 8:00 and 9:00 o'clock:

rawdata["T08:00/T09:00"]

This shows all the rows in the dataset in the year 2011:

rawdata["2011"]

This shows all the rows on the 1st of January 2014:

rawdata["2014-01-01"]

We could directly use the transaction level data, however, we can start simpler with daily and monthly data. See how in the next post: Using xts to summarize Bitcoin transaction data

You can also jump to each section directly from here: