From m@ech|er @end|ng |rom @t@t@m@th@ethz@ch  Wed Feb  9 11:36:00 2022
From: m@ech|er @end|ng |rom @t@t@m@th@ethz@ch (Martin Maechler)
Date: Wed, 9 Feb 2022 11:36:00 +0100
Subject: [Rd] Bug in rbind.data.frame?
In-Reply-To: <25079.40418.973003.506232@hornik.net>
References: <6244c3b8-0750-c675-9353-92fc78cfd02f@gmail.com>
 <90da4fd6-2e80-1b7b-4e36-5895138fa42b@gmail.com>
 <25079.40418.973003.506232@hornik.net>
Message-ID: <25091.39184.290122.386634@stat.math.ethz.ch>

>>>>> 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



[Rd] Bug in rbind.data.frame?
Martin Maechler