[R] negative indexing with null index sets
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Jun 5 22:10:34 CEST 2008
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.
>
More information about the R-help
mailing list