[R] Vector indices and minus sign

Patrick Burns pburns at pburns.seanet.com
Fri Nov 14 14:27:49 CET 2003


S Poetry, page 72.

If there is nothing there, then R can't know that
you want the nothing to be negative.

Patrick Burns

Burns Statistics
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>




More information about the R-help mailing list