Why is xts implemented as a matrix rather than a data frame?
Pergunta
I've found some information about xts fields representation in this thread but I'm still not clear why xts is an index + a matrix. Why not an index + a data frame? Wouldn't that allow more flexibility when working with factors and numeric columns?
Once I've loaded data in xts most of the work consists in performing numerical operations on a full set or a subset of the time series. For this the indexing works very well, but I am forced to go through calls like data.frame(data.matrix(myxts))
to be able to extract factors and numerical columns.
In addition I find more convenient to use the $ notation than the matrix indexing, although this is really a different question. For example:
lm(myxts$Res ~ myxts$ThisVar + myxts$ThatVar)
is easier to write than
lm(myxts[, "Res"] ~ myxts[, "ThisVar"] + myxts[, "ThatVar"]).
Solução
xts uses matrix rather than data.frame because:
- it is a subclass of zoo, and that's how zoo objects are structured, and
- matrix objects have much better performance than data.frames.
Your second question could be solved by using the data=
argument to lm
or, more generally, by using with
:
with(myxts, lm(Res ~ ThisVar + ThatVar))