2.5 Financial Data

Financial data are usually time series.

For time series data, there are two data formats in R: ts (time series) and xts (extensible time series). The former needs not have time stamp and can be converted from a vector directly. The latter takes date and time seriously so that it can only be defined with date and/or time columns.

2.5.1 Time Series data

The function ts converts any vector into time series data.

price <- c(2,3,7,8,9,12,8,7)
price <- ts(price)
price
## Time Series:
## Start = 1 
## End = 8 
## Frequency = 1 
## [1]  2  3  7  8  9 12  8  7

It can also convert any numeric dataframe into time series dataframe.

df <- data.frame(a = c(3,4,5),
                 b = c(1,2,4),
                 d = c(1,2,3))
df <- ts(df)
df
## Time Series:
## Start = 1 
## End = 3 
## Frequency = 1 
##   a b d
## 1 3 1 1
## 2 4 2 2
## 3 5 4 3