[Rd] Bug in rbind.data.frame?

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Wed Feb 9 11:36:00 CET 2022


>>>>> Kurt Hornik 
>>>>>     on Mon, 31 Jan 2022 09:29:22 +0100 writes:

>>>>> Duncan Murdoch writes:
    >> Okay, I spotted it.  This is intentional.  From ?rbind.data.frame:
    >> "The rbind data frame method first drops all zero-column and zero-row 
    >> arguments."

    > Hmm.  "As documented", but still surprising too me as well ...

    > We also say

    > For ‘rbind’ column names are taken from the first argument with
    > appropriate names: colnames for a matrix, or names for a vector of
    > length the number of columns of the result.

    > Of course, one could argue that "The rbind data frame method first drops
    > all zero-column and zero-row arguments." implies that "first argument
    > ..." should be taken after dropping, but then

    R> m <- matrix(0, 0, 2, dimnames = list(NULL, c("a", "b")))
    R> rbind(m, c(3, 4))
    >      a b
    > [1,] 3 4

    > which is not consistent with the data frame case.

(I agree and I think we should even consider to change
 rbind.data.frame() there  ... )

    > Btw, whereas

    R> rbind(c(1, 2), c(3, 4, 5))
    > Warning in rbind(c(1, 2), c(3, 4, 5)) :
    > number of columns of result is not a multiple of vector length (arg 1)
    >      [,1] [,2] [,3]
    > [1,]    1    2    1
    > [2,]    3    4    5

    > "as documented", 

    R> df <- data.frame(a = 1, b = 2)
    > rbind(df, c(3, 4, 5))
    >   a b
    > 1 1 2
    > 2 3 4

    > with is a bit worrying (and not as documented)?

Kurt and I have continued to talk about this,
and  few minutes ago, I've committed a change to R-devel's
rbind.data.frame()

which now gives

> rbind(data.frame(a = 1, b = 2), c(3, 4, 5))
  a b
1 1 2
2 3 4
Warning message:
In rbind(deparse.level, ...) :
  number of columns of result, 2, is not a multiple of vector length 3 of arg 2
> 

i.e., the same result, but *with* an informative warning,
analogously to the warning that has been produce "forever" in
the matrix case.

Martin



More information about the R-devel mailing list