如何在xts中索引一分钟的日内数据? [英] How to index one minute intraday data in xts?
问题描述
我使用 quantmod 处理每日股票数据.Quantmod 自动从 google/yahoo 金融网站下载数据,并自动转换为 xts 对象作为索引日期.
I have worked with daily stock data using quantmod. Quantmod automatically dowloads data from google/yahoo finance sites and convert automatically to a xts object as date as the index.
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2014-10-01 100.59 100.69 98.70 99.18 51491300 97.09741
2014-10-02 99.27 100.22 98.04 99.90 47757800 97.80230
2014-10-03 99.44 100.21 99.04 99.62 43469600 97.52818
2014-10-06 99.95 100.65 99.42 99.62 37051200 97.52818
2014-10-07 99.43 100.12 98.73 98.75 42094200 96.67644
2014-10-08 98.76 101.11 98.31 100.80 57404700 98.68340
2014-10-09 101.54 102.38 100.61 101.02 77376500 98.89877
现在我正在处理一分钟持续时间的日内数据(csv 格式),我将其转换为六列的数据框(df).
Now I am woking with intraday data(csv format) of one minute duration which I converted to a data frame(df) of six column.
Date Time Open High Low Close
1 20150408 09:17:00 7.15 7.15 7.10 7.10
2 20150408 09:18:00 7.15 7.15 7.15 7.15
3 20150408 09:19:00 7.10 7.10 7.10 7.10
4 20150408 09:20:00 7.10 7.10 7.05 7.10
5 20150408 09:21:00 7.10 7.15 7.10 7.10
6 20150408 09:22:00 7.10 7.10 7.05 7.10
现在如何将此数据帧转换为时间序列,以便我可以将其与默认的 quantmod 函数一起使用,例如 Cl()、Op()、OHLC() 等.
Now how to convert this dataframe to a time series in such a way that I can use it with the default quantmod functions such as Cl(),Op(),OHLC() etc.
推荐答案
Elementary,亲爱的 Watson:将日期和时间组合成一个 POSIXct
,使用那个.
Elementary, dear Watson: combine date and time into a POSIXct
, use that.
未经测试,因为您没有提供可重现的数据:
Untested as you supplied no reproducible data:
pt <- as.POSIXct(paste(X$Date, X$Time), format="%Y%m%d %H:%M:%S")
N <- xts(X[, -(1:2)], order.by=pt)
这里X
是你当前的data.frame,N
是一个新的xts
对象,由X
(减去日期和时间)使用 pt
作为索引.
Here X
is your current data.frame, and N
is a new xts
object formed from the data of X
(minus date and time) using pt
as the index.
这篇关于如何在xts中索引一分钟的日内数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!