我试图将csv转换为xts,但我不知道我得到了什么。当我运行第一行时,我得到一个数据帧,然后我使用第二行转换为xts。但是当我运行第二行时,日期列中的数据会在同一列中重复出现!
sti <- read.csv('C:/.../STI.csv', header = TRUE)
sti_xts <- as.xts(sti,order.by = as.Date(sti$DATE))
DATE OPEN HIGH LOW CLOSE VOLUME X12.prd.VMAVE
2013-01-02 "2013-01-02" "1.0" "1.1" "1.0" "1.0" "16,194,000.00" "11,209,083.00"
2013-01-03 "2013-01-03" "1.1" "1.1" "1.0" "1.0" "12,803,000.00" "10,935,667.00"
2013-01-04 "2013-01-04" "1.0" "1.1" "1.0" "1.0" "15,133,000.00" "10,335,167.00"
2013-01-07 "2013-01-07" "1.0" "1.0" "1.0" "1.0" "13,289,000.00" "11,284,750.00"
2013-01-08 "2013-01-08" "1.0" "1.0" "1.0" "1.0" "57,941,000.00" "15,668,000.00"
2013-01-09 "2013-01-09" "1.0" "1.0" "1.0" "1.0" "81,389,000.00" "21,386,583.00"
我在使用R方面很陌生,而且在经过一些教程之后我一直在修补它,但我似乎无法想出这一点。
另外,有没有办法让我格式化我的csv文件,所以我不会遇到这样的问题?谢谢!
答案 0 :(得分:1)
首先,删除千位分隔符并转换为数字:
sti$VOLUME <- as.numeric(gsub(",", "", sti$VOLUME))
sti$prd.VMAVE <- as.numeric(gsub(",", "", sti$prd.VMAVE))
然后,当转换为xts时,DATE列不会重复,而是用作您的时间序列索引:
> index(sti)
[1] "2013-01-02" "2013-01-03" "2013-01-04" "2013-01-07" "2013-01-08" "2013-01-09"
您想要的可能是排除第一列:
> as.xts(sti[,-1],order.by = as.Date(sti$DATE))
OPEN HIGH LOW CLOSE VOLUME prd.VMAVE
2013-01-02 1.0 1.1 1 1 16194000 11209083
2013-01-03 1.1 1.1 1 1 12803000 10935667
2013-01-04 1.0 1.1 1 1 15133000 10335167
2013-01-07 1.0 1.0 1 1 13289000 11284750
2013-01-08 1.0 1.0 1 1 57941000 15668000
2013-01-09 1.0 1.0 1 1 81389000 21386583