Rumen Kostadinov
2011-Feb-13 16:59 UTC
[R] Removing elements from a vector matching a criteria, BUG in which() function
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.
Jeff Newmiller
2011-Feb-13 17:37 UTC
[R] Removing elements from a vector matching a criteria, BUG in which() function
I think your rewrite is overdue, because returning the array from the which function seems counterintuitive. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Rumen Kostadinov <rkostadi@gmail.com> wrote: 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._____________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
Sarah Goslee
2011-Feb-13 17:37 UTC
[R] Removing elements from a vector matching a criteria, BUG in which() function
If by "bug" you mean "function behaving exactly as documented." which() returns only the matches, the TRUE values. If there are no matches, it doesn't return anything. If I understand what you are trying to do, and I may not, a[which(a != 5)] is really what you want, and it is precisely to preserve that behavior that which() does what it does. Sarah --- which package:base R Documentation Description: Give the ?TRUE? indices of a logical object, allowing for array indices. Value: If ?arr.ind == FALSE? (the default), an integer vector with ?length? equal to ?sum(x)?, i.e., to the number of ?TRUE?s in ?x?; Basically, the result is ?(1:length(x))[x]?. --- On Sun, Feb 13, 2011 at 11:59 AM, Rumen Kostadinov <rkostadi at gmail.com> wrote:> 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. >-- Sarah Goslee http://www.functionaldiversity.org
Apparently Analagous Threads
- matching on two criteria
- Relative frequency of cases in data frame matching a specified criteria
- Loop question?
- How to find STRESS criteria in MDS when there are negative eigenvalues....
- Select the rows in a dataframe that matches a criteria in another dataframe