From p.dalgaard@biostat.ku.dk  Tue Sep 26 21:27:27 2000
From: p.dalgaard@biostat.ku.dk (Peter Dalgaard BSA)
Date: 26 Sep 2000 22:27:27 +0200
Subject: [Rd] Re: subset()  wish: allow "select" for non-dataframes
In-Reply-To: Martin Maechler's message of "Tue, 26 Sep 2000 21:52:47 +0200"
References: <14800.65167.494938.453452@gargle.gargle.HOWL>
Message-ID: <x266nic30w.fsf@blueberry.kubism.ku.dk>

Martin Maechler <maechler@stat.math.ethz.ch> writes:

> {Mainly to the author of the subset() function,
>  but RFC to all "R-develers": }

Man, that was many moons ago...

> Wouldn't it be worth redefining  subset.default() such
> that `anything reasonable' would allow a select argument?
> 
> Currently, only subset.data.frame() allows a select argument,
> and hence we can have
> 
>   > (x <- c(a=1, b=3, d= 7:10))
>    a  b d1 d2 d3 d4 
>    1  3  7  8  9 10 
> 
>   > subset(as.data.frame(rbind(x)), select = -(d2:d3))
>     a b d1 d4
>   x 1 3  7 10
> 
> {and ``subset(as.data.frame(rbind(x)))'' might already be a sensible
>  definition for  subset.default  at least 
>  if(!missing(select) && !is.null(names(x))); 
>  and similarly for matrices.
> }

This sounds doable.

> 2nd thought: 
>       One could even start thinking if we should n't even enhance
>       "[" and "[[" instead of just  subset()...
> 
> I agree that one might not like things like   x[- d1] that are
> undefined when not part of the index, but then 
> "i , j" is also not really defined when not inside "[ .. ]" ...

You do need to be careful since x[i] is ambiguous if "i" is both a
variable name in the environment and the name of an element of x. One
idea could be to allow indexing with unevaluated calls like
x[quote(-(d2:d3))] and internally have

if (is.language(ix)) {
    nl<-as.list(1:dim)
    names(nl)<-names(x)
    ix<-eval(ix,nl)
}

(good luck with [.data.frame &friends, though...)
-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] Re: subset() wish: allow "select" for non-dataframes
Peter Dalgaard BSA