showkey
showkey

Reputation: 298

how to make which(x) run when all FALSE in x?

> x<-c(FALSE,FALSE)
> which(x)
integer(0)
> which(x)==0
logical(0)
> x<-c(FALSE,TRUE)
> which(x)
[1] 2

In my program,i don't know what x is,x is a logical vector,maybe contain TRUE,if it contain TRUE,print the order,if it don't contain TRUE at all,print 0.

Integer(0) is not the same as 0?what is the difference?what is the meaning of logical(0)?
which(x) can not do,when no TRUE in x ,which(x) can do,when there is TRUE in x. how can i fulfil my target?

Upvotes: 0

Views: 68

Answers (1)

MrFlick
MrFlick

Reputation: 206401

You can check if any value in the vector x is true with

any(x)

rather than which() so

if (any(x)) {
    #print order
} else {
    print(0)
}

Upvotes: 3

Related Questions