简体   繁体   中英

Select a column of an xts to manipulate in R

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM