简体   繁体   English

为什么我的 xts 对象在索引字段中有一个 X?

[英]Why does my xts object have an X in the index field?

Say I have a basic xts object.假设我有一个基本的xts对象。

a <- xts(order.by = Sys.Date()-1:5,(6:10))
a
           [,1]
2019-04-10   10
2019-04-11    9
2019-04-12    8
2019-04-13    7
2019-04-14    6

However I sometimes get this:但是我有时会得到这个:

       [,1]
X2019-04-10   10
X2019-04-11    9
X2019-04-12    8
X2019-04-13    7
X2019-04-14    6

It looks like the data has an "X" in front.看起来数据前面有一个“X”。

If I try to see the index I get:如果我尝试查看索引,我会得到:

index(a)[1]
"2019-04-10"

but if I have an "X "I get:但如果我有一个“X”,我会得到:

index(a)[1]
"2019-04-10 UTC"

I am using source R data created by someone else.我正在使用其他人创建的源 R 数据。 As far as I can tell, it is always of the same format, but when I have the "X", the xts does not behave as expected especially with merge or rbind especially with normal data.据我所知,它总是具有相同的格式,但是当我有“X”时, xts行为不像预期的那样,尤其是对于合并或 rbind,尤其是对于普通数据。

Update:更新:

Here is the head of the dput(Xdata) as requested.这是请求的dput(Xdata)的头部。

dput(head(temp))
structure(c(116.625, 116.34375, 116.460938, 116.296875, 116.265625, 
116.4375), class = c("xts", "zoo"), .indexCLASS = c("POSIXlt", 
"POSIXt"), .indexTZ = "UTC", tclass = c("POSIXlt", "POSIXt"), tzone = "UTC", index = structure(c(1511913600, 
1.512e+09, 1512086400, 1512345600, 1512432000, 1512518400), tzone = "UTC", tclass = c("POSIXlt", 
"POSIXt")), .Dim = c(6L, 1L), .Dimnames = list(NULL, "Close.Price"))
           Close.Price
2017-11-29     116.6250
2017-11-30     116.3438
2017-12-01     116.4609
2017-12-04     116.2969
2017-12-05     116.2656
2017-12-06     116.4375

The only thing that looks strange to me is that one of the index values is 1.512e+09 instead of the full integer.唯一让我觉得奇怪的是索引值之一是 1.512e+09 而不是完整整数。 I have no idea how to fix that.我不知道如何解决这个问题。

Update 2: The X only shows up in Rstudio if using the View command.更新 2:如果使用 View 命令,X 只会出现在 Rstudio 中。

X2018.02.22     114.2188
X2018.02.23     114.3828
X2018.02.26     114.4375
X2018.02.27     114.1484
X2018.02.27.1   114.1484

If I look at the index itself the only difference is that the X has UTC displayed in the index.如果我查看索引本身,唯一的区别是 X 在索引中显示了 UTC。

I think that the duplicate entry with 2018.02.27.1 is responsible for the XI would expect it to simply be a duplicated date.我认为 2018.02.27.1 的重复条目是 XI 的原因,希望它只是一个重复的日期。 If I remove that second date with head() the X goes away.如果我用 head() 删除第二个日期,X 就会消失。 I am using R 3.5.3 and xts 0.11-2.我正在使用 R 3.5.3 和 xts 0.11-2。 I recently updated R and all packages.我最近更新了 R 和所有软件包。

I think I finally got it.我想我终于明白了。 After using Cettt's code to clean the index, I used: temp = temp[!duplicated(index(temp))] to remove extra duplicates and finally the X is gone.使用 Cettt 的代码清理索引后,我使用: temp = temp[!duplicated(index(temp))]删除额外的重复项,最后 X 消失了。 I think the original data has some corruption.我认为原始数据有一些损坏。 And these steps clean it up.这些步骤将其清理干净。

Update 4: I found a bigger issue with the X's.更新 4:我发现 X 有一个更大的问题。 If I output the data frame with fwrite the index is printed with the X. This adversely affects output file readability and usability.如果我用 fwrite 输出数据帧,索引会用 X 打印。这会对输出文件的可读性和可用性产生不利影响。 Removing duplicates is not a good solution as sometimes duplicates are necessary.删除重复项不是一个好的解决方案,因为有时需要重复项。 I do this regularly and never encountered these issues before.我经常这样做,以前从未遇到过这些问题。 My packages seem to match others without issue.我的包裹似乎与其他包裹匹配没有问题。 I can only assume it is my Rstuio version 1.2.1335 that is the problem.我只能假设这是我的 Rstuio 版本 1.2.1335 的问题。 Can anyone think of a solution?谁能想到解决办法?

Update 5 The issue also happens with Rgui 3.5.3 with View() (which I did not know it had) so the problem is not with Rstuio, but with R itself???更新 5这个问题也发生在 Rgui 3.5.3 和 View()(我不知道它有)所以问题不在于 Rstuio,而在于 R 本身???

Update 6 I used a clean machine and tried different versions of R. R 3.4.4 with xts 0.11-2 and zoo 1.8-5 did not have the same issue.更新 6我使用了一台干净的机器并尝试了不同版本的 R. R 3.4.4 与 xts 0.11-2 和动物园 1.8-5 没有相同的问题。 3.4.4 did not have a native View() function so it required Rstudio. 3.4.4 没有原生的 View() 函数,所以它需要 Rstudio。 In R 3.5.x with the same package versions View() is available in the Rgui and they all display and print the X.在具有相同包版本的 R 3.5.x 中,Rgui 中提供了 View(),它们都显示和打印 X。

我不知道“X”从何而来(我无法重现这种行为),但也许您可以像这样摆脱它:

index(df) <- as.Date(format(index(df), tz = ""))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM