Re: [R] negative indexing with null index sets

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu, 05 Jun 2008 16:10:34 -0400

Here are a few possibilities. The first three use seq_along and the last two use if and length:

x[ !seq_along(x) %in% a[s] ]

x[ !match(seq_along(x), a[s], 0) ]

x[ setdiff(seq_along(x), a[s]) ]

if (length(a[s])) x[-a[s]] else x

x[ if (length(a[s])) -a[s] else TRUE ]

On Thu, Jun 5, 2008 at 3:16 PM, roger koenker <rkoenker_at_uiuc.edu> wrote:
> Negative indexing is often handy, but I'm in need of an appropriate idiom
> for handling cases in which the index set can be null:
>
> x <- rnorm(5)
> a <- 1:5
> s <- rep(FALSE,5)
> y <- x[-a[s]]
>
> # I'd like y == x but instead one has x[-a[s]] == x[a[s]] == numeric(0),
> which is rather
> # unfortunate -- so far the best I have come up with is:
>
> as <- ifelse(length(a[s]),-a[s],TRUE)
> y <- x[as]
>
> which, doesn't read well. Apologies if this has been aired previously...
>
> url: www.econ.uiuc.edu/~roger Roger Koenker
> email rkoenker_at_uiuc.edu Department of Economics
> vox: 217-333-4558 University of Illinois
> fax: 217-244-6678 Champaign, IL 61820
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 05 Jun 2008 - 21:32:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 05 Jun 2008 - 22:30:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive