我怎样才能得到“我的”呢;“数据槽”;从一个给定的Rs3对象?
出于好奇,我想知道是否有可能提取“数据”的内容,就像我应用R函数“str”时看到的那样。 我可以用attr()提取属性,但是“数据”呢 例如:我怎样才能得到“我的”呢;“数据槽”;从一个给定的Rs3对象?,r,R,出于好奇,我想知道是否有可能提取“数据”的内容,就像我应用R函数“str”时看到的那样。 我可以用attr()提取属性,但是“数据”呢 例如: library(PerformanceAnalytics) data(edhec) str(edhec) An ‘xts’ object on 1997-01-31/2009-08-31 containing: **Data**: num [1:152, 1:13] 0.0119 0.0123 0.0078 0.0086 0.0156 0.02
library(PerformanceAnalytics)
data(edhec)
str(edhec)
An ‘xts’ object on 1997-01-31/2009-08-31 containing:
**Data**: num [1:152, 1:13] 0.0119 0.0123 0.0078 0.0086 0.0156 0.0212 0.0193 0.0134 0.0122 0.01 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:13] "Convertible Arbitrage" "CTA Global" "Distressed Securities" "Emerging Markets" ...
Indexed by objects of class: [Date] TZ: GMT
xts Attributes:
NULL
如何获取“数据”?(我知道coredata()会做这项工作,但我的问题是一个一般性的R问题)。例如,attr(edhec,“index”)会给我属性“index”,但我如何访问“数据”?将其作为“一般R问题”表明您尚未理解R是一种基于对象类的函数分派的函数语言<代码>xts-对象没有“插槽”;这是一个s4r概念。“**Data**
”-文本是由print.xts
-函数生成的信息性标签
class(edhec) .... returns .. [1] "xts" "zoo"
在它们的“核心”,zoo和xts对象实际上都是二维的,即类似矩阵的对象。但是,它们确实具有专门定义的属性,例如print
和str
。要查看当前工作区中可用的str
-方法,请执行以下操作
methods(str)
[1] str.data.frame* str.Date* str.default* str.dendrogram*
[5] str.dictionary* str.Formula* str.gtable* str.igraph*
[9] str.logLik* str.POSIXt* str.quosure* str.Rcpp_stack_trace*
[13] str.uneval* str.unit.arithmetic* str.xts* str.zoo*
see '?methods' for accessing help and source code
相反,如果我想要正交方法,即列出为xts
-对象定义的所有函数,我会这样做:
methods(class="xts")
[1] [ [<- align.time as.complex as.data.frame
[6] as.double as.environment as.integer as.list as.logical
[11] as.matrix as.numeric as.POSIXct as.POSIXlt as.ts
[16] as.xts as.zoo as.zooreg c cbind
[21] CLASS<- coredata cummax cummin cumprod
[26] cumsum diff dimnames dimnames<- end
[31] first index index<- indexClass<- indexFormat<-
[36] indexTZ indexTZ<- is.time.unique lag last
[41] lines make.index.unique merge na.locf na.omit
[46] Ops plot points print rbind
[51] rollapply split start str tclass<-
[56] time<- tzone tzone<- xtsAttributes<-
see '?methods' for accessing help and source code
以及:
以及:
请注意,试图获得对以下问题的回答:“一个xts
-对象真的是一个R矩阵吗”,我得到:
尽管xts对象在大多数情况下的行为都类似于R矩阵,因为它具有dim
-ensional属性:
dim(edhec)
#[1] 152 13
edhec[ 1, ]
#---------
Convertible Arbitrage CTA Global Distressed Securities Emerging Markets
1997-01-31 0.0119 0.0393 0.0178 0.0791
Equity Market Neutral Event Driven Fixed Income Arbitrage Global Macro
1997-01-31 0.0189 0.0213 0.0191 0.0573
Long/Short Equity Merger Arbitrage Relative Value Short Selling Funds of Funds
1997-01-31 0.0281 0.015 0.018 -0.0166 0.0317
因此,xts对象既是xts
对象,也是zoo
对象,它们都有自己的访问器和操作功能集。“[”函数(可能)是您请求的提取函数。没有“数据”元素或Data
函数。当然,有coredata
函数是您不想要的:
> is.matrix(coredata(edhec))
[1] TRUE
如果
x
是一个S3对象,那么这将删除离开数据的所有属性,x0
:
x0 <- x
attributes(x0) <- NULL
x0edhec
数据集未包含在xts
包中。很抱歉,它当然应该是库(PerformanceAnalytics),请编辑并删除我的下一票:)as.data.frame(edhec)
?这是一个一般性的R问题,不要关注xts。在xts以外的对象中,as.data.frame不一定有意义。也许您可以在回答的开头更清楚地说明,xts
对象中*没有数据*槽或属性**。(现在,它在末尾…)类似地,如何列出现有的插槽或属性?它甚至对S3对象有意义吗?S3对象(各种类别)通常都有属性。事实上,zoo
和xts
对象都是S3对象。它们没有的是“插槽”,这是一个S4 R概念。换句话说,我需要一个解决方案,没有直接访问。这就是我想知道的。非常感谢Gabor。
> inherits(edhec, "matrix")
[1] FALSE
dim(edhec)
#[1] 152 13
edhec[ 1, ]
#---------
Convertible Arbitrage CTA Global Distressed Securities Emerging Markets
1997-01-31 0.0119 0.0393 0.0178 0.0791
Equity Market Neutral Event Driven Fixed Income Arbitrage Global Macro
1997-01-31 0.0189 0.0213 0.0191 0.0573
Long/Short Equity Merger Arbitrage Relative Value Short Selling Funds of Funds
1997-01-31 0.0281 0.015 0.018 -0.0166 0.0317
> is.matrix(coredata(edhec))
[1] TRUE
x0 <- x
attributes(x0) <- NULL
x0 <- x
attnames <- names(attributes(x0))
special <- c("names", "dim", "dimnames")
attributes(x0)[setdiff(attnames, special)] <- NULL