A simple query returns 3 columns: instrument, date, price
Data as follows:
library("xts")
dta = data.frame(
sample(x=c("a", "b", "c", "d", "e", "f"), size=367, replace=TRUE),
c(as.Date('2016-01-01') + 0:366),
c(0:366))
names(dta) <- c("instr", "date", "price")
What I want is a pivoted xts-object that looks as follows:
"a" "b" "c" "d" "e" "f" 2016-01-01 1 NA NA NA NA NA 2016-01-02 NA 2 NA NA NA NA 2016-01-03 3 NA NA NA NA NA 2016-01-04 NA NA NA NA 4 NA ...
I was thinking about splitting the data into "instrument"-vectors:
list_of_instr_vectors <- split(dta, dta$instr)
Turn all the data.frames of the list into xts-objects and merge.xts those.
Maybe it would be simpler to turn the data.frame from the start into an xts:
xts_dta = as.xts(dta, order.by = dta$date)
And pivot that data... but how?
I am sure, there is a simple way to accomplish this common task... but how?
Thanks for any hint! Marco