[Rd] How to put my class into data.frame

Gregor Gorjanc gregor.gorjanc at bfro.uni-lj.si
Sat Nov 4 02:09:13 CET 2006


Ferdinand Alimadhi wrote:
> tmp$comp <- x at ratios   ?!

I "lose" class here and that is not what I want. Perhaps I should stick
with list instead of data.frame.

> 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!

-- 
Lep pozdrav / With regards,
    Gregor Gorjanc
----------------------------------------------------------------------
University of Ljubljana     PhD student
Biotechnical Faculty
Zootechnical Department     URI: http://www.bfro.uni-lj.si/MR/ggorjan
Groblje 3                   mail: gregor.gorjanc <at> bfro.uni-lj.si

SI-1230 Domzale             tel: +386 (0)1 72 17 861
Slovenia, Europe            fax: +386 (0)1 72 17 888

----------------------------------------------------------------------
"One must learn by doing the thing; for though you think you know it,
 you have no certainty until you try." Sophocles ~ 450 B.C.



More information about the R-devel mailing list