[R] Vector indices and minus sign
Thomas W Blackwell
tblackw at umich.edu
Fri Nov 14 14:47:37 CET 2003
David -
I had to try your example verbatim before I understood what is
happening. index <- numeric() creates a vector with no entries.
Therefore the subscript is neither positive or negative, rather
it contains no numeric values, so the return contains no entries
either. Works the same in R-1.7.1 (which I am still running).
Not sure whether they will be equivalent to what your grep()
construct is doing, but take a look at help("unique"),
help(duplicated). I use those all the time.
- tom blackwell - u michigna medical school - ann arbor -
On Fri, 14 Nov 2003, David Orme wrote:
> 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