Skip to content

subset() wish: allow "select" for non-dataframes

2 messages · Martin Maechler, Peter Dalgaard

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

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.
}

---

Using  ranges and "-" in indices for names instead of just integers 
is a feature that I have been missing for a long time.
It would allow much more expressive code in some situations.


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 "[ .. ]" ...

--
[Evening musings;  maybe I'm too close to dream already...]

Martin


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Martin Maechler <maechler@stat.math.ethz.ch> writes:
Man, that was many moons ago...
This sounds doable.
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...)