I'm having trouble creating an xts object for multi-dimensional panel data. The data looks like this:
Object Factor Quarter Units
A x 01.01.2013 255
A x 01.04.2013 240
A x 01.07.2013 310
A x 01.10.2013 420
A x 01.01.2014 520
A y 01.01.2013 170
A y 01.04.2013 160
A y 01.07.2013 207
A y 01.10.2013 280
A y 01.01.2014 347
B x 01.01.2013 1199
B x 01.04.2013 1330
B x 01.07.2013 1450
B x 01.10.2013 1214
B x 01.01.2014 1429
B y 01.01.2013 827
B y 01.04.2013 764
B y 01.07.2013 924
B y 01.10.2013 844
B y 01.01.2014 893
Orienting myself at this post here, my R code looks like this:
library(xts)
Data2 <- read.csv('TestData.csv')
Data2List <- split(Data2, Data2$Object)
xtsData2 <- lapply(Data2List, function(x){
attrCol <- c("Object", "Factor")
numCol <- c("Units")
y <- xts(x[,numCol], as.Date(x$Quarter, format = '%d.%m.%Y'))
xtsAttributes(y) <- as.list(x[1,attrCol])
y
})
Summary of A looks like this, to me this looks fine:
str(xtsData2$A)
An ‘xts’ object on 2013-01-01/2014-01-01 containing:
Data: int [1:10, 1] 255 170 240 160 310 207 420 280 520 347
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
$ Object: Factor w/ 2 levels "A","B": 1
$ Factor: Factor w/ 2 levels "x","y": 1
But somehow, the periodicity is messed up:
> periodicity(xtsData2$A)
0 seconds periodicity from 2013-01-01 to 2014-01-01
This is also visible in the axTicksByTime function:
axTicksByTime(xtsData2$A)
Jan 01 00:00:00 Apr 01 00:00:00 Jul 01 00:00:00 Okt 01 00:00:00 Jan 01 00:00:00 Jan 01 00:00:00
1 3 5 7 9 10
When I enter the data without the Factor column, everything works fine and the periodicity is correctly displayed as quarterly.
How do I correctly enter my multi-dimensional data with R still recognizing the data format?