+3 votes
1 view
in R Programming by (7.3k points)
edited by

Let's say we have a statement that produces integer(0), e.g.

 a <- which(1:3 == 5)

What is the safest way of catching this?

2 Answers

+2 votes
by (25.4k points)
edited by

To catch integer(0) produced by any statement, you can use the length() function as follows:

 a <- which(1:3 == 5)

> a

integer(0)

length(a)

[1] 0

Therefore you can test “a” for being of length zero.

If you want to explore more in R programming then watch this R programming tutorial for beginner:

+3 votes
by (108k points)

The way of printing a zero-length vector(an integer one) is as follows:

You could test for length 0

length(a)

[1] 0

I suggest you not to use 'which' and try using the above method.

It might be worth rethinking the approach you are using to identify which elements you want, but without further specific details, it is difficult to suggest an alternative strategy.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...