[R] Removing elements from a vector matching a criteria, BUG in which() function

Rumen Kostadinov rkostadi at gmail.com
Sun Feb 13 17:59:23 CET 2011


Dear all,

I found a bug in the which() function.

When trying to remove elements with the which function,
if the criteria is not matched, numeric(0) is returned instead of the
array itself.

This is very weird.

> a = c(1,2,3,4,5)
> a[!a==6]
[1] 1 2 3 4 5
> a[-which(a==6)]
numeric(0)
> a[-which(a==5)]
[1] 1 2 3 4
> a[!a==5]
[1] 1 2 3 4

Is this correct? I believe this is a bug.

I have to rewrite a lot of my R code to use
a = a[!criteria]
and not
a = a[-which(criteria)]

R.



More information about the R-help mailing list