How to Test When Condition Returns Numeric(0) in R

How to test when condition returns numeric(0) in R

You could use ?length:

isEmpty <- function(x) {

input <- c(3, 12);

if (!isEmpty(setdiff(input, 1:9))) {
stop ("not valid")

How to return NA instead of numeric(0) when conditions do not return a result in R

If your always expecting exactly one value, you could do this:

item_size <- c(data_size$size[data_size$Item == item_code], NA)[1]

How to use numeric(0) in greater than testing?

What is supposed to happen when you compare a number with an empty value? Should the condition x > y evaluate to TRUE or FALSE, or should there be a different effect? One way to handle the situation is like this:

if (length(y) == 0) {
cat("y is numeric(0)")
} else {
if (x > y) {
cat("x is greater than y")
} else {
cat("x is less than or equal y")

Check if value == integer(0) in R

You can use identical which is the safe and reliable way to test two objects for being exactly equal (from the docs):

value = integer(0)

identical(value, integer(0))
# [1] TRUE

Or do the following check:

is.integer(value) && length(value) == 0
# [1] TRUE

R: ifelse turns numeric(0) into NA

you can access the implementation of ifelse, which is

function (test, yes, no) 
if (is.atomic(test)) {
if (typeof(test) != "logical")
storage.mode(test) <- "logical"
if (length(test) == 1 && is.null(attributes(test))) {
#... let's skip this part..
else test <- if (isS4(test))
methods::as(test, "logical")
else as.logical(test)
ans <- test
len <- length(ans)
ypos <- which(test)
npos <- which(!test)
if (length(ypos) > 0L)
ans[ypos] <- rep(yes, length.out = len)[ypos]
if (length(npos) > 0L)
ans[npos] <- rep(no, length.out = len)[npos]
<bytecode: 0x00000123e6b7d3a0>
<environment: namespace:base>

So, yes, it is because ifelse is vectorized - specifically along the condition - and the return object ans is initialized as a vector of the same length as the condition.

The description of ifelse states

ifelse returns a value with the same shape as test which is filled
with elements selected from either yes or no depending on whether the
element of test is TRUE or FALSE.

Let test <- TRUE. The interesting lines are

ypos <- which(test)
rep(numeric(0), length.out = 1)[ypos]

How to catch integer(0)?

That is R's way of printing a zero length vector (an integer one), so you could test for a being of length 0:

R> length(a)
[1] 0

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

R “argument is of length zero” in if(condition)

If data is a one dimension array (the binary data you showed is) rather then a matrix, vector, or other structure you can use length to find the row count. This code shows how to add a count based on matches with a one dimensional array (note I assume inc is a pascal like custom function).

data = c(1, 0, 0, 1, 1, 0)
outcome = c(0, 0, 1, 0, 1, 0)

count <- 0
for (i in 1:length(data))
if(outcome[i]==data[i]) {
count <- count + 1 #could then do inc(count) for package Hmisc

If your data is being returned as a matrix of vectors then try this code (same note on Hmisc).:

data <- read.table(text = "
0 1 0 1
0 0 1 1
1 1 0 0
1 0 1 0
0 1 0 1", header = FALSE)
data <- as.matrix(data)

outcome <- c(0, 0, 1, 0, 1)

count <- 0

for (i in 1:nrow(data))
if(!is.null(outcome[i]==data[i, 1])) {
count <- count + 1 #see first example

Note: nrow does return either an integer or NULL

Related Topics

Leave a reply