TransWikia.com

Column header in XTS and Data frame in R

Data Science Asked by Balasubrahmanyam Ira on September 2, 2020

I used quandl function extract stock data with object type as xts.

a <- Quandl("NSE/ICICIBANK", start_date = "2011-01-01", end_date = "2019-02-21", collapse = "daily", type = "xts",order = "asc") 

then I printed the output a with head(a),
the output is

             Open    High     Low    Last   Close Total Trade Quantity Turnover (Lacs)
2011-01-03 1154.00 1158.40 1138.95 1140.35 1144.85              1379646        15845.58
2011-01-04 1145.70 1149.20 1098.10 1107.25 1104.05              4175490        46519.73
2011-01-05 1097.20 1099.00 1065.00 1068.80 1069.35              5307600        57075.28
2011-01-06 1074.25 1078.45 1047.25 1050.00 1053.45              4769781        50447.94
2011-01-07 1045.00 1077.90 1041.00 1042.00 1049.20              6178882        65700.46
2011-01-10 1044.25 1058.85 1005.10 1012.05 1014.00              6510410        67029.00

then I did the same without the object type as xts,
the output of head(a):

       Date    Open    High     Low    Last   Close Total Trade Quantity Turnover (Lacs)
1 2011-01-03 1154.00 1158.40 1138.95 1140.35 1144.85              1379646        15845.58
2 2011-01-04 1145.70 1149.20 1098.10 1107.25 1104.05              4175490        46519.73
3 2011-01-05 1097.20 1099.00 1065.00 1068.80 1069.35              5307600        57075.28
4 2011-01-06 1074.25 1078.45 1047.25 1050.00 1053.45              4769781        50447.94
5 2011-01-07 1045.00 1077.90 1041.00 1042.00 1049.20              6178882        65700.46
6 2011-01-10 1044.25 1058.85 1005.10 1012.05 1014.00              6510410        67029.00

Why the date column field heading is missed out in xts object type but in data frame date field column heading is appearing?
How to handle the same in xts object type?

One Answer

Because an R xts object stores the date-column in a special column called index, and the programmers who implemented the xts package have decided to not print a label such as "Date" in the table header of an xts output.

When the print() method is called with an xts object as argument, the first column of the output is actually printed without any label, like a rowname "column" of a data frame.

Why? I don't know. Perhaps "Date" is not always right. It could just be a different label that your time series needs (e.g. "Microseconds" for some timeseries object of Sensor measurements)

# returned as data frame by Quandl
 R> str(a)

    'data.frame':   1985 obs. of  8 variables:
     $ Date                : Date, format: "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" ...
     $ Open                : num  1154 1146 1097 1074 1045 ...
     $ High                : num  1158 1149 1099 1078 1078 ...
     $ Low                 : num  1139 1098 1065 1047 1041 ...
     $ Last                : num  1140 1107 1069 1050 1042 ...
     $ Close               : num  1145 1104 1069 1053 1049 ...
     $ Total Trade Quantity: num  1379646 4175490 5307600 4769781 6178882 ...
     $ Turnover (Lacs)     : num  15846 46520 57075 50448 65700 ...
     - attr(*, "freq")= chr "daily"

# returned as xts timeseries object

    R> str(ax)
    An ‘xts’ object on 2011-01-03/2019-01-04 containing:
      Data: num [1:1985, 1:7] 1154 1146 1097 1074 1045 ...
     - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : chr [1:7] "Open" "High" "Low" "Last" ...
      Indexed by objects of class: [Date] TZ: UTC
      xts Attributes:  
     NULL

Answered by knb on September 2, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP