0

I have the following headings:

"Index","Open","High","Low","Close","Volume"

I only want to use the Close volume in the table called prices, how would I only select the column close so I can only use that data?

I have tried something along the lines of

closePrices <- prices("Close") 

and

closePrices <- prices[Close]

both of which throw an error

eden
  • 43
  • 5

1 Answers1

2

There are a few options to do this. See the examples below.

library(xts)
library(quantmod)

data(sample_matrix)
sample_xts <- as.xts(sample_matrix)

based on column name

xts_close <- sample_xts[, "Close"]

head(xts_close)
              Close
2007-01-02 50.11778
2007-01-03 50.39767
2007-01-04 50.33236
2007-01-05 50.33459
2007-01-06 50.18112
2007-01-07 49.99185

or

xts_close <- sample_xts$Close

head(xts_close)
              Close
2007-01-02 50.11778
2007-01-03 50.39767
2007-01-04 50.33236
2007-01-05 50.33459
2007-01-06 50.18112
2007-01-07 49.99185

using quantmod

# using quantmod::Cl
quant_close <- Cl(sample_xts)

head(quant_close)
              Close
2007-01-02 50.11778
2007-01-03 50.39767
2007-01-04 50.33236
2007-01-05 50.33459
2007-01-06 50.18112
2007-01-07 49.99185

all.equal(xts_close, quant_close)

[1] TRUE
phiver
  • 23,048
  • 14
  • 44
  • 56