Re: [R] How to convert S4 class slots into data.frame or how to assign variables of type 'Date'

From: Ronny Wölbing <ronny.woelbing_at_mac.com>
Date: Fri, 21 Nov 2008 21:10:37 +0100

> Hi,
>
> I created a class (S4) with some slots like value, date, description
> (it's actually a financial transaction class). Now I need a method
> to convert this class forth and back into a single row data.frame,
> where every slots represents a column. This method looks at the
> moment like this:
>
> > setMethod("as.data.frame", "Transaction",
> function(x, row.names = NULL, optional = FALSE, ...){
> slotnames <- slotNames(x)
> slotlist <-
> data.frame(rbind(1:length(slotnames)))
> names(slotlist) <- slotnames
> for(i in slotnames) {
> slotlist[1, i] <- slot(x, i)
> }
> return(slotlist)
> }
> )
>
> This method doesn't require predetermined slotnames or types, which
> is important to me. The method works quite good but the problem is
> that I have slots of type 'Date' and this method doesn't preserve
> the type but converts it to numeric.
>
> A couple of tests showed that this is actually a problem of
> assigning values to data.frame column. Something like this:
>
> > slotlist$DayOfTransaction <- slot(Transaction, DateOfTransaction)
>
> would preserve the type of DateOfTransaction as 'Date'.
>
> But I don't see a way to use this assigning scheme in my method
> without using the actual slotnames and giving up a lot of flexibility.
>
> Do you have any suggestions? Is there maybe even a simple way to
> convert S4 slots into data.frames?
>
> Thanks in advance
> Ronny
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Does nobody has an idea?



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 21 Nov 2008 - 20:44:25 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 21 Nov 2008 - 22:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive