[Rd] bug in rbind.data.frame: wrong rownames (PR#8506)

deepayan.sarkar@gmail.com deepayan.sarkar at gmail.com
Thu Jan 19 20:47:29 CET 2006


Hi,

there is a bug in the calculation of row names in rbind.data.frame.
When one of the arguments has 0 rows but is named in the call, this
mistakenly contributes an element in the "row.names" attribute of the
result, e.g.:

> foo <- data.frame(x = 1:10, y = rnorm(10))
> bar1 <- rbind.data.frame(foo[1:5,], foo[numeric(0),])
> dim(bar1)
[1] 5 2
> bar2 <- rbind.data.frame(a = foo[1:5,], b = foo[numeric(0),])
> dim(bar2)
[1] 6 2
> sessionInfo()
Version 2.3.0 Under development (unstable) (2006-01-17 r37109)
i686-pc-linux-gnu

attached base packages:
[1] "methods"   "stats"     "graphics"  "grDevices" "utils"     "datasets"
[7] "base"

This happens because

> rownames(bar1)
[1] "1" "2" "3" "4" "5"
> rownames(bar2)
[1] "a.1" "a.2" "a.3" "a.4" "a.5" "b"

I think the following patch (to the 'Make.row.names' function defined
inside rbind.data.frame) fixes this:

-Deepayan

Index: src/library/base/R/dataframe.R
===================================================================
--- src/library/base/R/dataframe.R      (revision 37109)
+++ src/library/base/R/dataframe.R      (working copy)
@@ -886,7 +886,7 @@
        if(nchar(nmi) > 0) {
            if(ni > 1)
                paste(nmi, ri, sep = ".")
-           else nmi
+           else nmi[ri]
        }
        else if(nrow > 0 && identical(ri, 1:ni))
            seq(from = nrow + 1, length = ni)



More information about the R-devel mailing list