[Rd] Bug in rbind.data.frame?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sun Jan 30 13:00:39 CET 2022


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

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



More information about the R-devel mailing list