0

I am following along with the FAQ here: https://joshuaulrich.github.io/xts/xts_faq.html

I make this sample.xts

sample.xts <- xts(1:6, as.POSIXct(c("2009-09-22 07:43:30",
  "2009-10-01 03:50:30", "2009-10-01 08:45:00", "2009-10-01 09:48:15",
  "2009-11-11 10:30:30", "2009-11-11 11:12:45")))
# align index into regular (e.g. 3-hour) blocks
aligned.xts <- align.time(sample.xts, n=60*60*3)
# apply your function to each block
count <- period.apply(aligned.xts, endpoints(aligned.xts, "hours", 3), length)
# create an empty xts object with the desired regular index
empty.xts <- xts(, seq(start(aligned.xts), end(aligned.xts), by="3 hours"))
# merge the counts with the empty object
head(out1 <- merge(empty.xts, count))

Then I assign a symbol:

sample.xts <- as.xts(transform(sample.xts, Symbol="AAPL"))

> sample.xts
                    V1  Symbol
2009-09-22 07:43:30 "1" "AAPL"
2009-10-01 03:50:30 "2" "AAPL"
2009-10-01 08:45:00 "3" "AAPL"
2009-10-01 09:48:15 "4" "AAPL"
2009-11-11 10:30:30 "5" "AAPL"
2009-11-11 11:12:45 "6" "AAPL"

My problem is that now, the data is all strings.

I assume this is because the underlying data is a matrix, so all the data items need to be the same type. Is there a coredata representation that lets me have string Symbols as well as posix Dates as well as OHLC doubles? Repeated with the data(sample_matrix):

> data(sample_matrix)
> sample.xts <- as.xts(sample_matrix)
> head(sample.xts)
               Open     High      Low    Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.23050 50.42188 50.23050 50.39767
2007-01-04 50.42096 50.42096 50.26414 50.33236
2007-01-05 50.37347 50.37347 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185

> sample.xts$Symbol <- "AAPL"
Warning message:
In merge.xts(..., all = all, fill = fill, suffixes = suffixes) :
  NAs introduced by coercion

> head(sample.xts)
               Open     High      Low    Close Symbol
2007-01-02 50.03978 50.11778 49.95041 50.11778     NA
2007-01-03 50.23050 50.42188 50.23050 50.39767     NA
2007-01-04 50.42096 50.42096 50.26414 50.33236     NA
2007-01-05 50.37347 50.37347 50.22103 50.33459     NA
2007-01-06 50.24433 50.24433 50.11121 50.18112     NA
2007-01-07 50.13211 50.21561 49.99185 49.99185     NA
Mittenchops
  • 18,633
  • 33
  • 128
  • 246

1 Answers1

0

You are correct that as soon as you add the symbol to an xts it will transform into a character matrix. If you want to have the data in a data.frame (or tibble) you need to transform the xts into a data.frame.

Basic way, using functions when xts is loaded (R 4.0):

my_df <- data.frame(date = index(sample.xts), # gets the date index from the xts object
                    coredata(sample.xts), # gets the matrix from the xts object
                    symbol = "aapl")

When working with multiple stocks, inside a data.frame use tq_get from tidyquant to get the data from e.g. yahoo, tiingo or alphavantage.

To switch between data.frame / tibble and xts, the timetk package has all sorts of functions to do this. tk_tbl would transform a xts matrix into a tibble.

With both of these packages are a lot of vignettes to go through.

phiver
  • 23,048
  • 14
  • 44
  • 56