[Rd] Bug in rbind.data.frame?

Kurt Hornik Kurt@Horn|k @end|ng |rom wu@@c@@t
Mon Jan 31 09:29:22 CET 2022


>>>>> 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 document", 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.

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

Best
-k

> So I wouldn't expect to keep the names in the argument that was dropped.

> However, there's still an issue in the case where the row is named.  To 
> me it implies that when df is empty, rbind(df, c(a=1, b=2)) should be 
> the same as rbind(c(a=1, b=2)), which gives a properly named result.  Of 
> course, rbind(c(a=1, b=2)) won't call rbind.data.frame() at all, so that 
> explains the difference, but not the intention.

> Duncan Murdoch

> On 30/01/2022 6:50 a.m., Duncan Murdoch wrote:
>> I was surprised to see this result:
>> 
>> # This works:  Create a dataframe and add a row:
>> df <- data.frame(a = 1, b = 2)
>> rbind(df, c(3, 4))
>> #>   a b
>> #> 1 1 2
>> #> 2 3 4
>> 
>> # It doesn't work if the original dataframe is empty
>> df <- data.frame(a = numeric(), b = numeric())
>> rbind(df, c(1, 2))
>> #>   X1 X2
>> #> 1  1  2
>> # The column names changed!
>> 
>> # It doesn't matter if the new row is named:
>> rbind(df, c(a = 1, b = 2))
>> #>   X1 X2
>> #> 1  1  2
>> 
>> I tried this in a very old R version, and saw the same result, so it's
>> not a new bug:  but is it maybe intentional, and if so, what is the
>> reason for it?
>> 
>> Duncan Murdoch

> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list