I'm having trouble viewing a data table in a tabular format. If I use the PCRA package and export a dataset as an xts object (see the minimal example below), the xts object resembles a table:
class(stocksDat)
[1] "xts" "zoo"
> head(stocksDat[, 1:3])
AAN ABM ABT
1993-01-31 0.28947368 -0.11250000 -0.08971193
1993-02-28 -0.04081633 0.14893617 -0.05000000
1993-03-31 -0.08510638 0.01234568 -0.01435407
1993-04-30 -0.04651163 0.03658536 0.08912621
1993-05-31 0.08829268 -0.08875740 -0.02690583
1993-06-30 -0.01123596 -0.10389610 -0.05529954
But if I export it as a data.table (second part of minimal example), I get a 3 column output:
> class(stocksDat2)
[1] "data.table" "data.frame"
> stocksDat2
TickerLast Date Return
1: AAN 1993-01-31 0.28947368
2: ABM 1993-01-31 -0.11250000
3: ABT 1993-01-31 -0.08971193
4: ADBE 1993-01-31 0.31999999
5: ADI 1993-01-31 0.01538462
---
81140: WGO 2015-12-31 -0.11555557
81141: WHR 2015-12-31 -0.09629589
81142: WMT 2015-12-31 0.05013595
81143: WTS 2015-12-31 -0.10520627
81144: XOM 2015-12-31 -0.04543236
What should I be doing to get the data.table to print just like the xts object, with a column of dates followed by columns of returns for each of the tickers? Converting the data.table to a data. Frame does not help. Is the PCRA output incorrectly formatted?
Sincerely and with many thanks in advance
Thomas Philips
Minimal example: Install package PCRA from CRAN
library(PCRA)
library(xts)
stockItems <- c("Date","TickerLast","Return")
# No need to specify begin and end dates if you want all
# Get the data as an xts object (the default)
stocksDat <- selectCRSPandSPGMI("monthly",
stockItems = stockItems,
factorItems = NULL,
subsetType = NULL,
subsetValues = NULL,
outputType = "xts")
names(stocksDat)
dim(stocksDat)
head(stocksDat[,1:3])
range(index(stocksDat)) # To see begin and end dates
# Now for a data.table output
stocksDat2 <- selectCRSPandSPGMI("monthly",
stockItems = stockItems,
factorItems = NULL,
subsetType = NULL,
subsetValues = NULL,
outputType = "data.table")
names(stocksDat2)
class(stocksDat2)
dim(stocksDat2)
head(stocksDat2)