CSV数据转换为XTS [英] csv data convert to xts
问题描述
我有一个csv文件,其中包含来自nasdaq100的2016年的1分钟数据.我想将其转换为xts以与Quantstrat一起使用.导入后,它看起来像这样:
I have a csv file with 1 minute data from the nasdaq100 for the year 2016. I would like to convert it to xts to work with quantstrat. After the import it looks like this:
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
我使用代码
NASD_xts = xts(NASD, order.by=as.POSIXct(NASD$date, format="%d-%m-%y %H:%M"))
并获得此结果.
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"
我的问题是第一行中的NA
值,应该有时间.所以我没有合适的xts来与Quantstrat一起工作.
My problem is the NA
value in the first row, there should be the time. So I don't get the right xts to work on with quantstrat.
推荐答案
您对as.POSIXct
的format
参数不正确.并且您不应该在xts对象中包括date
列,因为日期时间已经包含在index属性中,并且xts对象只能包含一个类型(因为它们是下面的矩阵).
Your format
argument to as.POSIXct
is incorrect. And you should not include the date
column in your xts object because the datetimes are already included in the index attribute, and xts objects can only contain a single type (because they are a matrix underneath).
包括date
列是使xts对象中其余列成为字符的原因.由于xts对象只能包含一种类型,因此data.frame的所有列都被强制转换为通用类型(在这种情况下为字符).
Including the date
column is what causes the rest of the columns in your xts object to be character. Since xts objects can only contain a single type, all columns of your data.frame are coerced to a common type (character in this case).
您的命令应为:
NASD_xts <- xts(NASD[,-1],
order.by = as.POSIXct(NASD$date, format = "%d.%m.%Y %H:%M"))
请注意,该格式假定date
列指定为月,日,年.您提供的样本数据中的月份和日期不明确.因此,真正的格式可以是日,月,年(这意味着日期可以是1月4日或4月1日).
Note that the format assumes the date
column is specified as month, day, year. The month and day are ambiguous in the sample data you provide. So the true format could be day, month, year (meaning the dates could be either January fourth, or April first).
这篇关于CSV数据转换为XTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!