...">

Configuring the xts index

Create an xts object with two lines.

library(xts)   
junk<-xts(c(1,2),as.Date(c("2010-01-01","2010-05-01")))   
junk   
>            [,1]
> 2010-01-01    1   
> 2010-05-01    2   

Why doesn't the index for the first row change?

time(junk[1])<-as.Date("2010-02-01")   
junk   
>            [,1]
> 2010-01-01    1   
> 2010-05-01    2   

I understand that the following works, but why does this not work?

time(junk)[1]<-as.Date("2010-02-01")   
junk   
>            [,1]
> 2010-02-01    1   
> 2010-05-01    2   

Thanks
Bill

+5
source share
3 answers

The direct response to the message is that the magic is inside attr<-, as Josh says. First, a subset of the object simply creates a new object, which is immediately deleted after the end of the time <-.

, "" .index(). , double integer, POSIXct, . , , . - - , .

, TZ , , - index(). .

+7

"[". dataframes. "" . , Date, DateTime, < POSIXct:

> attributes(junk)$index[1] <- as.POSIXct("2010-02-01")
> junk
           [,1]
2010-02-01    1
2010-05-01    2

: $index , .

+2
time(junk[1]) <- as.Date("2010-02-01") 

The above does not change the index of the first row junk, because the subset creates a new object - without reference to junk- and time<-.xtsreplaces the index of the new object.

+2
source

All Articles