[R] negative indexing with null index sets

From: roger koenker <rkoenker_at_uiuc.edu>
Date: Thu, 05 Jun 2008 14:16:52 -0500

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. Received on Thu 05 Jun 2008 - 20:46:16 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