How to put my class into data.frame
tmp$comp <- x at ratios ?!
Gregor Gorjanc wrote:
Hello!
I am developing a composition class, which represents a composition of
mixture of levels, say soil has three components and each component
accounts for 1/3 of the unit. I have tried with S4 class system and I
would appreciate any help here. I was hoping that I would be able to
include instance of my class into a data.frame. However, I encounter
problems.
Simple definition of class could be
setClass(Class="composition",
representation=representation(ratios="matrix"))
n <- 2
k <- 3
x <- new(Class="composition", ratios=matrix(data=rep(x=1/3, times=n*k),
nrow=n, ncol=k))
x
An object of class "composition"
Slot "ratios":
[,1] [,2] [,3]
[1,] 0.3333333 0.3333333 0.3333333
[2,] 0.3333333 0.3333333 0.3333333
tmp <- data.frame(id=1:2)
tmp$comp <- x
Error: object is not subsettable
As I understand this, my problem is that x is of length 1 as the
following works
tmp$comp <- c(x, x)
although I am not able to print tmp now
tmp
Error in unlist(x, recursive, use.names) :
argument not a list
but this is expected as I (probably) need show/print method for this.
How can I put my class into data.frame or how can I make my class behave
as a vector?
Thanks!
Ferdinand Alimadhi Programmer / Analyst Harvard University The Institute for Quantitative Social Science (617) 496-0187 falimadhi at iq.harvard.edu www.iq.harvard.edu