Hi, I tried to write the dim method for the list class, but R doesn't seem to dispatch to it:> dim.list = function(x) c(length(x[[1]]), length(x)) > dim(list(1))NULL> dim.list(list(1))[1] 1 1 What is the correct way of registering dim.list with .Primitive("dim")? Thanks, Vadim [[alternative HTML version deleted]]
On Sat, 7 May 2005, Vadim Ogranovich wrote:> I tried to write the dim method for the list class, but R doesn't seem > to dispatch to it: >> dim.list = function(x) c(length(x[[1]]), length(x)) >> dim(list(1)) > NULL >> dim.list(list(1)) > [1] 1 1 > > What is the correct way of registering dim.list with .Primitive("dim")?You really don't want to be doing this: the standard dim() function is intended to work with lists and is used e.g. for matrix lists. So you are in danger of breaking things. For that reason and for performance reasons, the internal dispatch only works on explicit class attributes as given by oldClass(), not the implicit ones given by class(). -- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On 5/7/05, Vadim Ogranovich <vograno@evafunds.com> wrote:> I tried to write the dim method for the list class, but R doesn't seem > to dispatch to it: > > dim.list = function(x) c(length(x[[1]]), length(x)) > > dim(list(1)) > NULL > > dim.list(list(1)) > [1] 1 1 > > What is the correct way of registering dim.list with .Primitive("dim")?The list method of dim is sealed> isSealedMethod("dim", "list")[1] TRUE but you could define a subclass, mylist, and use that:> setClass("mylist", representation(x = "list"), contains = "list")[1] "mylist"> setMethod("dim", "mylist", function(x) c(length(x@x[[1]]), length(x@x)))[1] "dim"> x <- new("mylist", x = list(x = 1, y = 2)) > dim(x)[1] 1 2
But then mylist is not a list:> x <- new("mylist", x = list(x = 1, y = 2)) > x[[1]]Error in x[[1]] : subscript out of bounds This is probably solvable by a sprinkle of setIs or setAs spells, but each time I get into the S4 area I feel walking a mine-field. All this exercise on my part was to write a list-based lightweight.data.frame class, which would be more efficient than the data.frame. I'd pondered this idea some time ago and now again came back to it frustrated by the slowness of the data.frame. But it seems I am not up to the task. Thank you for helping anyway, Vadim> -----Original Message----- > From: Gabor Grothendieck [mailto:ggrothendieck@gmail.com] > Sent: Saturday, May 07, 2005 8:56 AM > To: Vadim Ogranovich > Cc: r-devel@stat.math.ethz.ch > Subject: Re: [Rd] how to add method to .Primitive function > > On 5/7/05, Vadim Ogranovich <vograno@evafunds.com> wrote: > > I tried to write the dim method for the list class, but R > doesn't seem > > to dispatch to it: > > > dim.list = function(x) c(length(x[[1]]), length(x)) > > > dim(list(1)) > > NULL > > > dim.list(list(1)) > > [1] 1 1 > > > > What is the correct way of registering dim.list with > .Primitive("dim")? > > The list method of dim is sealed > > > isSealedMethod("dim", "list") > [1] TRUE > > but you could define a subclass, mylist, and use that: > > > setClass("mylist", representation(x = "list"), contains = "list") > [1] "mylist" > > setMethod("dim", "mylist", function(x) c(length(x@x[[1]]), > > length(x@x))) > [1] "dim" > > x <- new("mylist", x = list(x = 1, y = 2)) > > dim(x) > [1] 1 2 >
Thank you for the tip! I'll see if I can take it on to "[<-.mylist" Why do you think it will be hard to replace data frames? Insightful recently introduced a new class, largeDataFrame or something like this. This new class looks and feels like a data.frame, but they made two key simplifications: all columns are atomic vectors and there is no rownames. Maintaining the latter is a big overhead in many data.frame operations, consider for example rbind() which needs to ensure uniquness of the row names. I'd really like to hear why you think it would be a bad idea to have such a class. Thanks, Vadim> -----Original Message----- > From: Simon Urbanek [mailto:simon.urbanek@r-project.org] > Sent: Saturday, May 07, 2005 12:34 PM > To: Vadim Ogranovich > Cc: Gabor Grothendieck; r-devel@stat.math.ethz.ch > Subject: Re: [Rd] how to add method to .Primitive function > > On May 7, 2005, at 2:59 PM, Vadim Ogranovich wrote: > > > > But then mylist is not a list: > > > > > >> x <- new("mylist", x = list(x = 1, y = 2)) x[[1]] > >> > >> > > Error in x[[1]] : subscript out of bounds > > > > This is probably solvable by a sprinkle of setIs or setAs > spells, but > > each time I get into the S4 area I feel walking a mine-field. > > > > Well, then you can still use S3: > x <- list(x=1:5, y=5:1) > class(x) <- "mylist" > dim.mylist <- function(l) c(length(l[[1]]),length(l)) > dim(x) > [1] 5 2 > x[[1]] > [1] 1 2 3 4 5 > is.list(x) > [1] TRUE > > I'm not saying it's a good idea, because you can still break other > things if you're not careful, but it's possible... If all you > want is > writing convenience functions for lists, that's fine, but I don't > think you can replace data frames with such objects easily ... > > Cheers, > Simon > > >