简体   繁体   English

Xts转换错误(与对象长度不匹配)

[英]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.

相关问题 R错误:暗淡与物体长度不符 - R error: dims do not match the length of an object .xts索引长度中的错误必须与观察数匹配 - Error in .xts index length must match number of observations R xts NextMethod替换长度错误 - R xts NextMethod replacement length error data.frame对象到R中的xts对象转换 - data.frame object to xts object conversion in R dplyr总结str.default(obj,...)中的错误dims [product 11]与对象的长度不匹配[3] - dplyr summarise Error in str.default(obj, …) dims [product 11] do not match the length of object [3] dplyr,dunn测试,dim(robj)&lt;-c(dX,dY)中的错误:dims [产品0]与对象的长度不匹配 - dplyr, dunn test, Error in dim(robj) <- c(dX, dY) : dims [product 0] do not match the length of object R-交叉验证错误处理--“产品变暗与对象长度不匹配” - R - cross validation error handling-- "dims product do not match the length of object" 使用R函数“外部”时出现“尺寸[产品xx]与对象[xx]的长度不匹配”错误 - “dims [product xx] do not match the length of object [xx]” error in using R function `outer` 为什么 R 给我错误“dims [product 28550] do not match the length of object [1]” - Why is R giving me the error “dims [product 28550] do not match the length of object [1]” tbl_summary 错误:dims [产品 18] 与 object [27] 的长度不匹配 - tbl_summary error: dims [product 18] do not match the length of object [27]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM