Well, your question looks quite interesting to me.

*which *function normally returns a non-negative *integer *and if fails

to find the case, then returns integer(0) that is an integer with the zero

length. Logically it returns the right answer. Then, doing an operation on

nothing is pointless, however in mathematics, the complementary of nothing

is everything. Then you do not expect R to return the entire integer set! ;)

If you still like to use which, I advise you write a little function like

which0() that inherits the properties of *which *and checks for the special

cases.

Hamed.

Message: 1

Date: Tue, 8 Jan 2019 06:54:43 -0500

From: Duncan Murdoch <

[hidden email]>

To: Nick Wray <

[hidden email]>, r-help

<

[hidden email]>

Subject: Re: [R] Why does R do this?

Message-ID: <

[hidden email]>

Content-Type: text/plain; charset="utf-8"; Format="flowed"

On 08/01/2019 4:28 a.m., Nick Wray via R-help wrote:

> y<-c(1,2,3)

> z<-which(y>3)

At this point z is a vector with no entries in it.

> z

> y<-y[-z]

-z is the same vector. So y[z] and y[-z] are the same.

> y

>

> In the work I'm doing I often have this situation and have to make sure

that I condition on z being non-zero as y is now numeric(0) rather than the

set c(1,2,3). Why does R do this? Wouldn't it be more sensible for R to

simply leave the host set unchanged if there are no elements to take out?

No, it wouldn't. You asked for no entries, so you get no entries.

Follow Thierry's advice, and don't use which() unless you really need a

vector of indices, and are prepared for an empty one.

Duncan Murdoch

[[alternative HTML version deleted]]

______________________________________________

[hidden email] mailing list -- To UNSUBSCRIBE and more, see

https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide

http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.