How can I change XTS to data.frame and keep Index?

后端 未结 6 1999
时光说笑
时光说笑 2020-12-02 11:11

I have an XTS timeseries in R of the following format and am trying to do some processing, subsetting and re-arranging before exporting as a CSV for work in another program.

相关标签:
6条回答
  • 2020-12-02 11:50

    A elegant form to change XTS to data.frame:

    myDF <- as.data.frame(as.matrix(myXTS))
    
    0 讨论(0)
  • 2020-12-02 11:54

    Shane is right. you might be looking for index(your xts). Here's a reproducible example.

    library(xts)
    example(xts)
    x = head(sample.xts)
    datefield = index(x)
    newdf = data.frame(x,datefield)
    

    Then you should be able to simply export it to a csv. Of course you can rename the rows, too.

    0 讨论(0)
  • 2020-12-02 11:57

    You can convert an xts object to a data.frame that includes the index as a column named "Index" with zoo::fortify.zoo().

    You don't need ggplot2, but this will still work if you have xts (or zoo) and ggplot2 loaded.

    For example:

    library(xts)
    data(sample_matrix)
    x <- as.xts(sample_matrix, dateFormat = "Date")
    my_df <- fortify.zoo(x)
    head(my_df)
    #        Index     Open     High      Low    Close
    # 1 2007-01-02 50.03978 50.11778 49.95041 50.11778
    # 2 2007-01-03 50.23050 50.42188 50.23050 50.39767
    # 3 2007-01-04 50.42096 50.42096 50.26414 50.33236
    # 4 2007-01-05 50.37347 50.37347 50.22103 50.33459
    # 5 2007-01-06 50.24433 50.24433 50.11121 50.18112
    # 6 2007-01-07 50.13211 50.21561 49.99185 49.99185
    str(my_df)
    # 'data.frame': 180 obs. of  5 variables:
    #  $ Index: Date, format: "2007-01-02" "2007-01-03" ...
    #  $ Open : num  50 50.2 50.4 50.4 50.2 ...
    #  $ High : num  50.1 50.4 50.4 50.4 50.2 ...
    #  $ Low  : num  50 50.2 50.3 50.2 50.1 ...
    #  $ Close: num  50.1 50.4 50.3 50.3 50.2 ...
    
    0 讨论(0)
  • 2020-12-02 12:10

    This is a bit of a sidebar, but the fortify(...) function in package ggplot2 will convert a variety of objects to data frames suitable for use in ggplot(...), including xts objects.

    library(xts)
    set.seed(1)    # for reproducible example
    master_1 <- xts(rnorm(10,mean=2,sd=0.1),as.POSIXct("2010-03-03")+30*(0:9))
    
    library(ggplot2)
    df <- fortify(master_1)
    head(df)
    #                  Index master_1
    # 1  2010-03-03 00:00:00 1.937355
    # 2  2010-03-03 00:00:30 2.018364
    # 3  2010-03-03 00:01:00 1.916437
    # 4  2010-03-03 00:01:30 2.159528
    # 5  2010-03-03 00:02:00 2.032951
    # 6  2010-03-03 00:02:30 1.917953
    

    So if you're already using gggplot this is an easy way to do it. Note that the index goes into a column named Index (capital "I").

    0 讨论(0)
  • 2020-12-02 12:13

    That's because the dates are rownames in your data.frame. You need to make them a separate column.

    Try this:

     data.frame(date=index(master_1), coredata(master_1))
    
    0 讨论(0)
  • 2020-12-02 12:13

    Since 1.9.6 You can convert directly from/to xts without losing index class. As simple as:

    as.data.table(master_1)
    

    The index is added as the first column in the result data.table, it retains index Date or POSIXct classes.

    0 讨论(0)
提交回复
热议问题