[Rd] S4 class extending data.frame?

Ben Bolker bolker at ufl.edu
Thu Dec 13 06:04:41 CET 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I would like to build an S4 class that extends
a data frame, but includes several more slots.

Here's an example using integer as the base
class instead:

setClass("c1",representation(comment="character"),contains="integer")
z1 = new("c1",55,comment="hello")
z1
z1+10
z1[1]
z1 at comment

 -- in other words, it behaves exactly as an integer
for access and operations but happens to have another slot.

 If I do this with a data frame instead, it doesn't seem to work
at all.

setClass("c2",representation(comment="character"),contains="data.frame")
d = data.frame(1:3,2:4)
z2 = new("c2",d,comment="goodbye")
z2  ## data all gone!!
z2[,1]  ## Error ... object is not subsettable
z2 at comment  ## still there

  I can achieve approximately the same effect by
adding attributes, but I was hoping for the structure
of S4 classes ...

  Programming with Data and the R Language Definition
contain 2 references each to data frames, and neither of
them has allowed me to figure out this behavior.

 (While I'm at it: it would be wonderful to have
a "rich data frame" that could include as a column
any object that had an appropriate length and
[ method ... has anyone done anything in this direction?
?data.frame says the allowable types are
 "(numeric, logical, factor and character and so on)",
 but I'm having trouble sorting out what the limitations
are ...)

  hoping for enlightenment (it would be lovely to be
shown how to make this work, but a definitive statement
that it is impossible would be useful too).

  cheers
    Ben Bolker

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHYL1pc5UpGjwzenMRAqErAJ9jj1KgVVSGIf+DtK7Km/+JBaDu2QCaAkl/
eMi+WCEWK6FPpVMpUbo+RBQ=
=huvz
-----END PGP SIGNATURE-----



More information about the R-devel mailing list