[英]Xts conversion error (do not match the length of object)
I am importing a csv file into R, creating a 3x3 dataframe, and attempting to convert the dataframe to an xts object. 我将csv文件导入R,创建3x3数据框,并尝试将数据框转换为xts对象。 But I get error message "do not match the length of object". 但是我收到错误消息“对象长度不匹配”。
#DATSB <- fread("C:/Temp/GoogleDrive/R/temp.csv", select = c("DateTime","Last","Volume"))
#that results in following dput() output:
DATSB <- structure(list(DateTime = c("3/28/2016 20:37", "3/28/2016 20:36","3/28/2016 20:35"), Last = c(1221.7, 1221.8, 1221.9), Volume = c(14L,2L, 22L)), .Names = c("DateTime", "Last", "Volume"), row.names = c(NA,3L), class = "data.frame")
setDF(DATSB)
DATSB$DateTime <- strptime(DATSB$DateTime, format = "%m/%d/%Y %H:%M")
DATSBxts <- as.xts(DATSB[, -2], order.by = as.Date(DATSB$DateTime, "%Y/%m/%d %H:%M"))
DateTime Last Volume
1 3/28/2016 20:37 1221.7 14
2 3/28/2016 20:36 1221.8 2
3 3/28/2016 20:35 1221.9 22
Exact error message is "Error in as.matrix.data.frame(x) : dims [product 12] do not match the length of object [14]" 确切的错误消息是“ as.matrix.data.frame(x)中的错误:暗淡[产品12]与对象[14]的长度不匹配”
Somehow the root of the problem is the column Volume. 问题的根源是“卷”列。 Without that column, it works. 没有该列,它将起作用。 Unfortunately can't figure it out. 不幸的是无法弄清楚。 Thanks for your help! 谢谢你的帮助!
There was a typo here DATSB[, -2]
, correcting it works fine. 这里有一个错字DATSB[, -2]
,更正它可以正常工作。 General theme for xts is, xts的一般主题是
xts(data[,-date_column], order.by = data[,date_column])
Also coredata(DATSBxts)
and index(DATSBxts)
are helpful functions 同样, coredata(DATSBxts)
和index(DATSBxts)
是有用的功能
DATSBxts = xts(DATSB[, -1], order.by = DATSB[,1] ,dateFormat = "%Y/%m/%d %H:%M:%S");rev(DATSBxts)
DATSBxts
# Last Volume
#2016-03-28 20:35:00 1221.9 22
#2016-03-28 20:36:00 1221.8 2
#2016-03-28 20:37:00 1221.7 14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.