Reputation: 329
Using the unique command, I can easily get a list of unique tickers from my raw daily stock data. Which results in:
my.table <- unique(my.frame1[,5])
> my.table
[1] NPN BIL CFR IMP FSR SHF SHP REI INP OML REM ABL AGL SAB WHL BTI MMI RMI PIK TRU INL SPP CLS
[24] SBK PPC IPL SOL ASA RMH MTN ANG EXX DSY NED SLM AVI KIO VOD GFI APN
40 Levels: ABL AGL ANG APN ASA AVI BIL BTI CFR CLS DSY EXX FSR GFI IMP INL INP IPL KIO ... WHL
However, I now loop through the list to do a few basic calcs. I am trying to assign the ticker code to each trading day, but the observation in the list do not interact with my data as expected:
> code <- my.table[2]
> code
[1] BIL
40 Levels: ABL AGL ANG APN ASA AVI BIL BTI CFR CLS DSY EXX FSR GFI IMP INL INP IPL KIO ... WHL
> my.daily <- my.daily[,3:6]
> my.daily[,1] <- code
> my.daily
my.xts...1..Low my.xts...1..Close VWAP SIDE
2011-08-31 7 23765 23744.30 1
2011-09-08 7 22876 23056.72 1
2012-02-14 7 25050 25230.52 -1
2012-07-25 7 23480 23591.01 -1
2013-05-12 7 26818 26737.65 -1
Now I would have expected the first column in my xts object to be "BIL", as per the value of code above.
What am I missing please?
Upvotes: 0
Views: 750
Reputation: 121626
Your xts
is a numeric matrix. You can't have multi-types (numeric and factor) in a matrix. The factor is coerced to a factor (as already commented) to have a single type(numeric here).
As a workaround, you can firstly coerce your xts object to a data.frame.
my.daily <- as.data.frame(my.daily)
my.daily[,1] <- code
But why are you trying to do this? You loose all the power and efficiency of xts
to deal with time series.
Upvotes: 2