Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
2 views
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
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 beginners:

+2 votes
by (107k 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.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...