Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent rbind from creating empty rows when handling numeric(0)

Tags:

r

I find the behavior of rbind somewhat non-intuitive when it's provided with empty vectors. For example,

rbind(numeric(0), numeric(0), numeric(0))
# [1,]
# [2,]
# [3,]

produces a 3-by-0 matrix, while I would expect the output to be a consistent numeric(0) instead. While it may be fun to debate whether you get "one nothing" or "three nothings" when you add a "nothing" to itself three times, my question is whether it's possible to suppress this behavior? Alternatively, I am looking for another function that can "rbind" matrices, while also producing an object with no rows when none of the inputs have rows.

My approach so far has been to cast numeric(0) to a matrix(0,0,0), which appears to work more consistently with rbind:

rbind(matrix(0,0,0), matrix(0,0,0), matrix(0,0,0))
# <0 x 0 matrix>

High-level picture: I have a function that accepts one or more matrices representing linear constraints, rbinds them as the first step and performs some downstream operations. The number of rows in the rbind output is taken to be the total number of input constraints. I discovered the above behavior when I was removing the last row in an input matrix and accidentally left off drop=FALSE, thus calling my function with a numeric(0) instead of a proper matrix(0,0,0) and getting the wrong total number of constraints, because nrow(rbind(numeric(0))) was returning 1.

like image 573
Artem Sokolov Avatar asked Oct 28 '25 07:10

Artem Sokolov


1 Answers

I don't think you should expect there to be no rows. From rdocumentation, "numeric is identical to double (and real). It creates a double-precision vector of the specified length with each element equal to 0." So, I don't think it's accurate to say they are "nothings."

If you rbind NULL, which I think is closer to "nothing," you get no rows:

rbind(NULL, NULL, NULL)

returns

NULL

So, to answer the question, no I don't know how it would be possible to bind numeric and get no rows.

like image 89
Brian Syzdek Avatar answered Oct 30 '25 23:10

Brian Syzdek