简体   繁体   中英

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

Say I have a basic xts object.

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.

If I try to see the index I get:

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

but if I have an "X "I get:

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

I am using source R data created by someone else. 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.

Update:

Here is the head of the dput(Xdata) as requested.

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. I have no idea how to fix that.

Update 2: The X only shows up in Rstudio if using the View command.

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.

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. If I remove that second date with head() the X goes away. I am using R 3.5.3 and xts 0.11-2. I recently updated R and all packages.

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. 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. If I output the data frame with fwrite the index is printed with the X. This adversely affects output file readability and usability. 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. 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???

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. 3.4.4 did not have a native View() function so it required 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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