[R] negative indexing with null index sets

roger koenker rkoenker at uiuc.edu
Thu Jun 5 21:16:52 CEST 2008


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



More information about the R-help mailing list