从 xts 对象中提取 xts 属性
Extracting xts attributes from an xts object
假设一个 xts
对象 obj
,如下例所示:
library(quantmod)
getSymbols.FRED('USAPFCEQDSMEI', env = globalenv())
obj <- base::get('USAPFCEQDSMEI')
通过检查它的结构,str(obj)
,返回以下内容:
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"
我可以看到数据对象有一个包含 2 个 xts
属性的列表:src
和 updated
,但找不到提取这些属性的方法。经典的 unlist
或 obj$src
的 None 似乎有效。
在这种情况下如何正确提取 src
和 updated
字段?
您可以使用函数 xtsAttributes
访问这些属性。
xtsAttributes(obj)
$src
[1] "FRED"
$updated
[1] "2020-05-19 18:29:26 CEST"
或单独:
xtsAttributes(obj)$src
[1] "FRED"
xtsAttributes(obj)$updated
[1] "2020-05-19 18:29:26 CEST"
这是@phiver 展示的特殊 xtsAttributes
函数的基本 R 替代方法:
attributes(obj)$src
[1] "FRED"
attributes(obj)$updated
[1] "2020-05-19 12:37:31 EDT"
如help(xtsAttributes)
所述:
Since xts objects are S3 objects with special attributes, a method is necessary to properly assign and view the user-added attributes.
因此,请勿尝试:
###Don't do this###
#attributes(obj)$src <- "AAPL"
假设一个 xts
对象 obj
,如下例所示:
library(quantmod)
getSymbols.FRED('USAPFCEQDSMEI', env = globalenv())
obj <- base::get('USAPFCEQDSMEI')
通过检查它的结构,str(obj)
,返回以下内容:
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"
我可以看到数据对象有一个包含 2 个 xts
属性的列表:src
和 updated
,但找不到提取这些属性的方法。经典的 unlist
或 obj$src
的 None 似乎有效。
在这种情况下如何正确提取 src
和 updated
字段?
您可以使用函数 xtsAttributes
访问这些属性。
xtsAttributes(obj)
$src
[1] "FRED"
$updated
[1] "2020-05-19 18:29:26 CEST"
或单独:
xtsAttributes(obj)$src
[1] "FRED"
xtsAttributes(obj)$updated
[1] "2020-05-19 18:29:26 CEST"
这是@phiver 展示的特殊 xtsAttributes
函数的基本 R 替代方法:
attributes(obj)$src
[1] "FRED"
attributes(obj)$updated
[1] "2020-05-19 12:37:31 EDT"
如help(xtsAttributes)
所述:
Since xts objects are S3 objects with special attributes, a method is necessary to properly assign and view the user-added attributes.
因此,请勿尝试:
###Don't do this###
#attributes(obj)$src <- "AAPL"