1

In discusion bellow this post, there is comment:

Don't use which.

With several upvotes.

Why is using which in R bad and what are alternatives/preferable approaches?

Community
  • 1
  • 1
Zbynek
  • 5,673
  • 6
  • 30
  • 52
  • 1
    why don't you ask this question in that comment? – Raptor Feb 04 '14 at 06:57
  • I would like to preferably have more answers – Zbynek Feb 04 '14 at 07:01
  • 1
    I don't know why not `which()`, but `-which()`, particularly on a `data.frame` can yield some unexpected results sometimes. – A5C1D2H2I1M1N2O1R2T1 Feb 04 '14 at 08:42
  • I suspect the comment was heavily up-voted because [hadley](http://stackoverflow.com/users/16632/hadley) said it, and many people seem to take what he says as gospel (which is a criticism of them, not of Hadley). Using `which` was just an example of a statement that produces `integer(0)`. Whatever example the OP gave, someone could have said, "don't use that statement", which is entirely unhelpful to answering the actual question of how to catch a zero-length vector result. – Joshua Ulrich Feb 04 '14 at 11:57

1 Answers1

0

It should not be considered as a general recommendation, of course. It was just redundant in those conditions. I assume the problem was to catch the output of some function (or to use inside an if statement). If that's the case,

a <- which(1:3 == 5)
length(a) != 0

is the same as

b <- 1:3 == 5
any(b)

and the latter is clearly more elegant.

tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • Ok. I thought it was a general recommendation and perhaps `which` can be replaced by some more efficient alternative (like `apply` instead of `for`). Thanks – Zbynek Feb 04 '14 at 07:17
  • 1
    @Zbynek `apply` is not more efficient than `for` in terms of performance, only in terms of clarity of code. – Roland Feb 04 '14 at 08:02
  • My experience is that `apply` is much faster when i.e. calculating sum on rows of matrix - I have just tried that and it took only about 20% of time than for loop – Zbynek Feb 04 '14 at 08:09
  • 2
    @Zbynek If you consider that a significant performance difference ... Using `rowSums` will speed up things by orders of magnitude in this case. – Roland Feb 04 '14 at 08:21