[Rd] Light-weight data.frame class: was: how to add method to .Primitive function

Simon Urbanek simon.urbanek at r-project.org
Mon May 9 05:03:26 CEST 2005


Vadim,

On May 8, 2005, at 2:09 PM, Vadim Ogranovich wrote:

>> # the naive "[.lwdf" = function (x, i, j) lapply(x[j], function 
>> (col) col[i])

Umm... what about his:

"[.lwdf" = function(x, i, j) { r<-lapply(lapply(j,function(a) x 
[[a]]),function(x) x[i]); names(r)<-names(x)[j]; r }

The subsetting operates on vectors, so it's not a problem. Don't ask  
me about the speed, though ;). And btw: you could access "[

What I meant with my cautious remarks are the following issues. You  
were talking about building a df alternative (s/df/data.frame/g in  
this e-mail). The first issue is that by re-defining "[" and friends  
you make your new calls incompatible with the behavior of lists, so  
you won't be able to use it where lists are required (even though  
is.list says TRUE). This may break code were you'd like your class to  
act as a list. On the other hand, your class is not a df either - and  
I suspect that it's far from trivial to make it even closely  
compatible with a df in terms of its behavior. Moreover any function  
that checks for df won't treat your class as such, because it simply  
is no df (is.data.frame()=FALSE for starters). So in the end, you  
would have to modify every function in R that uses df to recognize  
your new class. On the other hand if you make your class a subclass  
of df (there we get into some trouble with S3), you could replace the  
back-end, but then you will have to support every df feature  
including row.names. You could try it, but I'm somewhat skeptical...  
but your mileage may vary ...

Cheers,
Simon



More information about the R-devel mailing list