[R] Vector indices and minus sign
David Orme
d.orme at imperial.ac.uk
Fri Nov 14 14:16:24 CET 2003
Hi,
I got caught out by this behaviour in 1.8.0 and I wondered why this
happens:
I have a list of vectors and was using lapply and grep to remove
matched elements that occur in only a subset of the elements of the
list:
locs <- lapply(locs, function(x){x[- grep("^x", x)]})
The problem is that where the grep finds no matches and hence returns a
vector of length 0, all the elements of x were removed, rather than
none. As a toy example:
vect <- 1:10
vect[-5]
index <- 5
vect[-index]
index <- numeric()
vect[-index] # I was expecting this to give all the elements of vect
rather than an empty vector
vect[index] # does the same thing
Thanks,
David
More information about the R-help
mailing list