R csv数据转换为xts
我有一个csv文件,其中包含2016年nasdaq100的1分钟数据。我想将其转换为xts以与quantstrat合作。导入后,它看起来如下所示:R csv数据转换为xts,r,csv,xts,R,Csv,Xts,我有一个csv文件,其中包含2016年nasdaq100的1分钟数据。我想将其转换为xts以与quantstrat合作。导入后,它看起来如下所示: date open high low close volume adjusted <chr> <dbl> <dbl> <dbl> <dbl> <int> <int>
date open high low close volume adjusted
<chr> <dbl> <dbl> <dbl> <dbl> <int> <int>
1 04.01.2016 14:30 48963818 48964272 48952363 48958789 0 0
2 04.01.2016 14:31 48923579 48940259 4891752 48940259 0 0
3 04.01.2016 14:32 48941753 48992466 48941753 48988589 0 0
4 04.01.2016 14:33 48992227 48992227 48948281 48965469 0 0
5 04.01.2016 14:34 48962915 4896418 48923838 48934326 0 0
6 04.01.2016 14:35 48931196 48963301 48931196 48954341 0 0
然后得到这个结果
date open high low close volume adjusted
<NA> "04.01.2016 14:30" "48963818" "48964272" "48952363" "48958789" " 0" "0"
<NA> "04.01.2016 14:31" "48923579" "48940259" " 4891752" "48940259" " 0" "0"
<NA> "04.01.2016 14:32" "48941753" "48992466" "48941753" "48988589" " 0" "0"
<NA> "04.01.2016 14:33" "48992227" "48992227" "48948281" "48965469" " 0" "0"
<NA> "04.01.2016 14:34" "48962915" " 4896418" "48923838" "48934326" " 0" "0"
<NA> "04.01.2016 14:35" "48931196" "48963301" "48931196" "48954341" " 0" "0"
调整日期开盘价高低收盘价
"04.01.2016 14:30" "48963818" "48964272" "48952363" "48958789" " 0" "0"
"04.01.2016 14:31" "48923579" "48940259" " 4891752" "48940259" " 0" "0"
"04.01.2016 14:32" "48941753" "48992466" "48941753" "48988589" " 0" "0"
"04.01.2016 14:33" "48992227" "48992227" "48948281" "48965469" " 0" "0"
"04.01.2016 14:34" "48962915" " 4896418" "48923838" "48934326" " 0" "0"
"04.01.2016 14:35" "48931196" "48963301" "48931196" "48954341" " 0" "0"
我的问题是第一行的
NA
值,应该有时间。因此,我无法使用quantstrat使用正确的xts。在列上应用xts
,除了“日期”列和顺序。按
和“日期”
library(xts)
library(lubridate)
xts(NASD[-1], order.by = mdy_hm(NASD$date))
# open high low close volume adjusted
#2016-04-01 14:30:00 48963818 48964272 48952363 48958789 0 0
#2016-04-01 14:31:00 48923579 48940259 4891752 48940259 0 0
#2016-04-01 14:32:00 48941753 48992466 48941753 48988589 0 0
#2016-04-01 14:33:00 48992227 48992227 48948281 48965469 0 0
#2016-04-01 14:34:00 48962915 4896418 48923838 48934326 0 0
#2016-04-01 14:35:00 48931196 48963301 48931196 48954341 0 0
数据
NASD您对as.POSIXct
的format
参数不正确。而且您不应该在xts对象中包含date
列,因为datetimes已经包含在index属性中,而且xts对象只能包含一种类型(因为它们下面是一个矩阵)
包含date
列是导致xts对象中其余列为字符的原因。由于xts对象只能包含一种类型,因此data.frame的所有列都强制为公共类型(本例中为字符)
您的命令应该是:
NASD_xts <- xts(NASD[,-1],
order.by = as.POSIXct(NASD$date, format = "%d.%m.%Y %H:%M"))
谢谢你的回答。我可以遵循第一步,但第二步我不明白。当我试图生成柱状图时,会出现错误:error in na.omit.xts(x):不支持的类型。我无法生成可以使用quantstrat的正确xts对象。我该怎么办??我尝试了不同的东西。你能帮我检查一下csv文件并试着用它做一个xts吗??会很有帮助的。
NASD <- structure(list(date = c("04.01.2016 14:30", "04.01.2016 14:31",
"04.01.2016 14:32", "04.01.2016 14:33", "04.01.2016 14:34", "04.01.2016 14:35"
), open = c(48963818L, 48923579L, 48941753L, 48992227L, 48962915L,
48931196L), high = c(48964272L, 48940259L, 48992466L, 48992227L,
4896418L, 48963301L), low = c(48952363L, 4891752L, 48941753L,
48948281L, 48923838L, 48931196L), close = c(48958789L, 48940259L,
48988589L, 48965469L, 48934326L, 48954341L), volume = c(0L, 0L,
0L, 0L, 0L, 0L), adjusted = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("date",
"open", "high", "low", "close", "volume", "adjusted"), row.names = c("1",
"2", "3", "4", "5", "6"), class = c("tbl_df", "tbl", "data.frame"
))
NASD_xts <- xts(NASD[,-1],
order.by = as.POSIXct(NASD$date, format = "%d.%m.%Y %H:%M"))