[R] using setMethod or setGeneric to change S4 accessor symbol from @ to $

Seth Falcon seth at userprimary.net
Thu Feb 11 00:24:38 CET 2010


Hi Markus,

On 2/9/10 4:12 PM, Markus Weisner wrote:
> Thanks so much for your help.  I am realizing that I may be
> over-complicating things for myself.  I have learned a ton about creating
> methods, but I feel like I am trying to reinvent the data.frame class.
> Basically, I am trying to create a data.frame type object where I can
> enforce the header names and column data types.  I am trying to force the
> user to setup the following fields:
>
>     - event_number (character)
>     - agency (factor)
>     - unit_num (factor)
>     - alarm (POSIXct)
>     - priority (factor)
>
> A user might use the following code:
>
> event_number = c(1:5)
> agency = c("CFD", rep("ACFR", 3), "CFD")
> unit_num = c("E1", "T10", "E3", "E2", "BC1")
> temp =  c("00:52:35", "06:58:18", "13:42:18", "20:59:45", "21:19:00")
> alarm = as.POSIXct(strptime(temp, format="%H:%M:%S"))
> priority = c("A", "E", "A", "C", "C")
> data = data.frame(event_number=event_number, agency=agency,
> unit_number=unit_num, alarm=alarm, priority=priority)
>
> I have all sorts of functions that I am trying to incorporate into a package
> for analyzing fire department data, but keep having problems with small
> deviations in data format causing errors.  In this example, the following
> might cause issues in my functions:
>
>     - "event_number" should be of type character
>     - "agency", "unit_number", and "priority", should be of type factor
>     - "unit_number" should actually have name "unit_num"

Perhaps you could simply provide a helper function for creating plain 
data.frames that does this validity checking?  You could also provide a 
validate function that takes a data.frame and either says "OK" or 
describes the ways in which the input does not conform.

I think the benefits you would get out of a complete S4 wrapping of 
data.frame do not outweigh the complexity introduced.

+ seth

-- 
Seth Falcon | @sfalcon | http://userprimary.net/user



More information about the R-help mailing list