Daniel Strauman
Daniel Strauman

Reputation: 23

How can i get the "Data slot" from a given R S3 object?

Out of curiosity, I wonder whether it's possible to extract content of "Data", as seen when I apply the R function "str". I can extract attributes with attr(), but what about "Data" ?

Example:

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

How to get "Data" ? (I know coredata() would do the job, but my question is a general R question). As an example, attr(edhec, "index") would give me the attribute "index", but how can I access "Data" ?

Upvotes: 1

Views: 1227

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269481

If x is an S3 object then this will remove all the attributes leaving the data, x0:

x0 <- x
attributes(x0) <- NULL

however, note that things like its names, dimensions and dimnames are attributes too so if you want to regard those as part of the data then you will need something like this:

x0 <- x
attnames <- names(attributes(x0))
special <- c("names", "dim", "dimnames")
attributes(x0)[setdiff(attnames, special)] <- NULL

Upvotes: 0

IRTFM
IRTFM

Reputation: 263331

Posing this as a "general R question" suggests you have not yet understood that R is a functional language with function dispatch based on class-es of objects. xts-objects do not have are "slots"; that is an S4 R concept. The "**Data**"-text is a an informational label produced by the print.xts-function.

class(edhec)  .... returns  .. [1] "xts" "zoo" 

At their "core", both zoo and xts objects are really just two-dimensional, i.e. matrix-like, objects. They do, however, have specially defined properties such as print and str. To see the str-methods which are available with my current workspace, I do this.

 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

If, instead, I wanted the orthogonal approach, i.e list all the functions that have been defined for xts-objects, I would do this:

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

Also look at the output of

 methods(class="zoo")

And:

attributes(edhec)

And:

dput(edhec)

Notice that attempting to get a response to the question: "is an xts-object really an R matrix", I get:

> inherits(edhec, "matrix")
[1] FALSE

This is despite the fact that an xts-object would behave in most ways like an R matrix, since it has dim-ensional attributes:

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

So xts-objects are both xts and zoo objects that have their own set of accessor and operational functions. The "[" function is (perhaps) the extraction function you requested. There is no "Data" element or Data function. There is, of course, the coredata function that you did not want:

> is.matrix(coredata(edhec))
[1] TRUE

Upvotes: 3

Related Questions