Daniel Murphy
2010-Jul-12 04:08 UTC
[Rd] S4 class extends "data.frame", getDataPart sees "list"
R-Devel: When I get the data part of an S4 class that contains="data.frame", it gives me a list, even when the "data.frame" is the S4 version:> d<-data.frame(x=1:3) > isS4(d)[1] FALSE # of course> dS4<-new("data.frame",d) > isS4(dS4)[1] TRUE # ok> class(dS4)[1] "data.frame" # good attr(,"package") [1] "methods"> setClass("A", representation(label="character"), contains="data.frame")[1] "A"> a<-new("A",dS4, label="myFrame") > getDataPart(a)[[1]] # oh? [1] 1 2 3> class(a@.Data)[1] "list" # hmm> names(a)[1] "x" # sure, that makes sense> aObject of class "A" x 1 1 2 2 3 3 Slot "label": [1] "myFrame" Was I wrong to have expected the "data part" of 'a' to be a "data.frame"? Thanks. Dan Murphy [[alternative HTML version deleted]]
John Chambers
2010-Jul-13 12:57 UTC
[Rd] S4 class extends "data.frame", getDataPart sees "list"
On 7/11/10 9:08 PM, Daniel Murphy wrote:> R-Devel: > > When I get the data part of an S4 class that contains="data.frame", it gives > me a list, even when the "data.frame" is the S4 version: > >> d<-data.frame(x=1:3) >> isS4(d) > [1] FALSE # of course >> dS4<-new("data.frame",d) >> isS4(dS4) > [1] TRUE # ok >> class(dS4) > [1] "data.frame" # good > attr(,"package") > [1] "methods" >> setClass("A", representation(label="character"), contains="data.frame") > [1] "A" >> a<-new("A",dS4, label="myFrame") >> getDataPart(a) > [[1]] # oh? > [1] 1 2 3 > >> class(a at .Data) > [1] "list" # hmm >> names(a) > [1] "x" # sure, that makes sense >> a > Object of class "A" > x > 1 1 > 2 2 > 3 3 > Slot "label": > [1] "myFrame" > > > Was I wrong to have expected the "data part" of 'a' to be a "data.frame"?Yes. Also, there is a clue in the documentation for getDataPart: "rarely suitable to be called directly" The data part, aka "data slot", generally does not have a class (S4 or S3). You are probably looking for S3Part(): > setClass("myFrame", contains = "data.frame") [1] "myFrame" > z = new("myFrame", data.frame(x=1:3)) > z Object of class "myFrame" x 1 1 2 2 3 3 > S3Part(z) Object of class "data.frame" x 1 1 2 2 3 3 > S3Part(z, strictS3 = TRUE) x 1 1 2 2 3 3> > Thanks. > > Dan Murphy > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >