using seq_along() to handle the empty case

Under the condition that df <- data.frame(), we have:

Case 1 falling victim to...

Error in .subset2(x, i, exact = exact) : subscript out of bounds

while Case 2 and 3 are not triggered.

In essence, the error in Case 1 is due to ncol(df) being 0. This leads the sequence 1:ncol(df) to be 1:0, which creates the vector c(1,0). In this case, the for loop tries to access the first element of the vector 1, which tries to access column 1 does not exist. Hence, the subset is found to be out of bounds.

Meanwhile, in Case 2 and 3 the for loop is never executed since there are no elements to process within their respective collections since the vectors are empty. Principally, this means that they have length of 0.

As this question specifically relates to what the heck is happening to seq_along(), let's take a traditional seq_along example by constructing a full vector a and seeing the results:

set.seed(111)
a <- runif(5)
seq_along(a)
#[1] 1 2 3 4 5

In essence, for each element of the vector a, there is a corresponding index that was created by seq_along to be accessed.

If we apply seq_along now to the empty df in the above case, we get:

seq_along(df)
# integer(0)

Thus, what was created was a zero length vector. Its mighty hard to move along a zero length vector.

Ergo, the Case 1 poorly protects the against the empty case.

Now, under the traditional assumption, that is there is some data within the data.frame, which is a very bad assumption for any kind of developer to make...

set.seed(1234)
df <- data.frame(matrix(rnorm(40), 4))

All three cases would be operating as expected. That is, you would receive a median per column of the data.frame.

[1] -0.5555419
[1] -0.4941011
[1] -0.4656169
[1] -0.605349 

标签