r-数据帧的Xts格式



我有一个数据帧,其中有两列。第一列包含POSIXct数据,第二列包含一些样本值整数。我一辈子都想不出如何将这个数据帧转换为xts。

我的数据:

structure(list(SampleDateTime = list(1422835200000, 1423353600000, 
1423958400000, 1433030400000, 1433635200000, 1434326400000, 
1434844800000, 1444521600000, 1445731200000, 1453593600000, 
1.455408e+12, 1420934400000, 1424563200000, 1425772800000, 
1426982400000, 1430006400000, 1.431216e+12, 1.440288e+12, 
1448150400000, 1460851200000), SampleValue = list(9L, 3L, 
2L, 1733L, 19L, 6L, 1L, 17L, 7L, 23L, 147L, 1L, 17L, 1L, 
1L, 19L, 1L, 11L, 2L, 91L), Dttm = structure(c(1107216000, 
1107734400, 1108339200, 1117411200, 1118016000, 1118707200, 1119225600, 
1128902400, 1130112000, 1137974400, 1139788800, 1105315200, 1108944000, 
1110153600, 1111363200, 1114387200, 1115596800, 1124668800, 1132531200, 
1145232000), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c("feature$attributes", 
"feature$attributes1", "feature$attributes2", "feature$attributes3", 
"feature$attributes4", "feature$attributes5", "feature$attributes6", 
"feature$attributes7", "feature$attributes8", "feature$attributes9", 
"feature$attributes10", "feature$attributes11", "feature$attributes12", 
"feature$attributes13", "feature$attributes14", "feature$attributes15", 
"feature$attributes16", "feature$attributes17", "feature$attributes18", 
"feature$attributes19"), class = "data.frame")
> 

我用这个来尝试进行转换:

q <- as.xts(t, order.by = t$Dttm, dateFormat="POSIXct", frequency=NULL, RECLASS=FALSE)

我得到以下错误:coredata.xts(x(中出错:当前不支持的数据类型

感谢您的帮助。我不明白为什么它说DF不受支持,而它说它们在文档中??

正如评论中所指出的,数据的格式很奇怪,数据帧的前两列本身就是列表,而不是数字列,因此首先取消列出每一列(这不会改变任何已经不是列表的列(,给出dat.u,然后进行转换。我们假设dat是问题中通过dput显示的数据。

dat.u <- replace(dat, TRUE, lapply(dat, unlist))
z <- read.zoo(dat.u, index = "Dttm")
x <- as.xts(z)

最新更新