Search code examples
rdataframedateformattingcolumnsorting

Formatting date column to row numbered (left side) column in dataframes in r


I have a problem where the dates are in column 1 of my first dataframe. I want them in the column with the row numbers like my second dataframe. Below is the dput code.


The two dput codes below show two different date settings. One of the dataframe settings show the dates that I have in column1 and the second dataframe has setting which what I want. The date column is in with the number columns and there is no title column for date.


Solution

  • I think you want to convert the dataframe into xts object. If the first dataframe is called df1 you can convert the Date column to date class and do :

    df2 <- xts::xts(df1[-1], as.Date(df1$Date))
    df2
    
    #            CC1G   HYLA   IHHG
    #2020-10-27 21328 545.65 473.52
    #2020-10-28 20915 539.00 469.95
    #2020-10-29 21050 538.10 469.55
    #2020-10-30 20723 538.15 470.90
    #2020-11-02 21305 539.00 471.82
    #2020-11-03 21430 544.35 474.60
    #2020-11-04 21938 549.40 480.95
    #2020-11-05 22048 555.50 482.80
    #2020-11-06 22043 555.15 481.97
    #2020-11-09 22693 562.50 486.05
    #2020-11-10 22555 557.10 483.95
    #2020-11-11 22928 556.45 483.67
    #2020-11-12 22860 554.90 470.77
    #2020-11-13 22360 555.60 470.80