Search code examples
rxts

Extracting xts attributes from an xts object


Assume an xts object obj as in the following example:

library(quantmod)
getSymbols.FRED('USAPFCEQDSMEI', env = globalenv())
obj <- base::get('USAPFCEQDSMEI')

By examining the structure of it, str(obj), the following is returned:

An ‘xts’ object on 1960-01-01/2020-01-01 containing:
  Data: num [1:241, 1] 8.16e+10 8.31e+10 8.30e+10 8.35e+10 8.36e+10 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "USAPFCEQDSMEI"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "FRED"
 $ updated: POSIXct[1:1], format: "2020-05-19 19:23:03"

I can see that there is a List of 2 xts attributes available with the data object: src and updated, but can't find a way to extract these. None of the classic unlist or obj$src seems to work.

How to properly extract src and updated fields in such case?


Solution

  • You can use the function xtsAttributes to access these attributes.

    xtsAttributes(obj)
    $src
    [1] "FRED"
    
    $updated
    [1] "2020-05-19 18:29:26 CEST"
    

    Or seperately:

    xtsAttributes(obj)$src
    [1] "FRED"
    
    xtsAttributes(obj)$updated
    [1] "2020-05-19 18:29:26 CEST"