I would like to have unique numeric factors as part of an xts, so that over time...each number refers to a specific factor, independent of time.
To give an example, imagine a stock index that changes its constituents every day. We can simulate this if I have the following universe of two letter stock tickers
universe <- apply(as.data.frame(expand.grid(letters,letters)),1,paste0,collapse="")
and each day an index is created that is a random subsample of 20 of the stock tickers from the universe.
subsample.list <- lapply(1:50, function(y){
sort(sample(universe,20,replace=FALSE))
})
the key of unique stocks over the 50 days is:
uni.subsample <- sort(unique(unlist(subsample.list)))
I would like to basically be able to see which stocks were in the index each day if i had the xts object and unique factors.
Although it is not meant to be used this way....I was thinking something like:
tmp <- xts(do.call(rbind,subsample.list),Sys.Date()-c(50:1))
to create the xts.
however I would like to covert the coredata into a numeric matrix, where each number is the ticker from uni.subsample
so if tmp.adjusted['20130716'][1,]
would be the numeric vector of numbers of length 20 that represents the numerical values of uni.subsample
for the 16th July 2013, so I would expect that I would be able to get all of 2013-07-16's index members by using the xts objecting the following way uni.subsample[tmp.adjusted['20130716'][1,]]
...i.e. the adjustment from tmp to tmp.adjusted converts the strings into factors, with unique levels associated with uni.subsample
I hope this makes sense...its kinda hard to explain....