I suspect there may be a bug in base::rbind.data.frame Below there is minimal example of the problem: m <- matrix (1:12, 3) dfm <- data.frame (c = 1 : 3, m = I (m)) str (dfm) m.names <- m rownames (m.names) <- letters [1:3] dfm.names <- data.frame (c = 1 : 3, m = I (m.names)) str (dfm.names) rbind (m, m.names) rbind (m.names, m) rbind (dfm, dfm.names) #not working rbind (dfm.names, dfm) Error in rbind(deparse.level, ...) : replacement has length zero rbind (dfm, dfm.names)$m [,1] [,2] [,3] [,4] <NA> 1 4 7 10 <NA> 2 5 8 11 <NA> 3 6 9 12 a 1 4 7 10 b 2 5 8 11 c 3 6 9 12 ________________________________ Important: This email is confidential and may be privileged. If you are not the intended recipient, please delete it and notify us immediately; you should not copy or use it for any purpose, nor disclose its contents to any other person. Thank you. [[alternative HTML version deleted]]
I'm not sure whether or not this is a bug, but I did isolate the line where the error is thrown: src/library/base/R/dataframe.R:1395. https://github.com/wch/r-source/blob/01374c3c367fa12f555fd354f735a6e16e5bd98e/src/library/base/R/dataframe.R#L1395 The error is thrown because the line attempts to set a subset of the rownames to NULL, which fails. R> options(error = recover) R> rbind(dfm.names, dfm) Error in rownames(value[[jj]])[ri] <- rownames(xij) : replacement has length zero Enter a frame number, or 0 to exit 1: rbind(dfm.names, dfm) 2: rbind(deparse.level, ...) Selection: 2 Called from: top level Browse[1]> rownames(value[[jj]]) [1] "a" "b" "c" NA NA NA Browse[1]> rownames(xij) NULL Browse[1]> ri [1] 4 5 6 Browse[1]> rownames(value[[jj]])[ri] [1] NA NA NA On Mon, Jan 16, 2017 at 7:50 PM, Krzysztof Banas <krzysztof at nus.edu.sg> wrote:> I suspect there may be a bug in base::rbind.data.frame > > Below there is minimal example of the problem: > > m <- matrix (1:12, 3) > dfm <- data.frame (c = 1 : 3, m = I (m)) > str (dfm) > > m.names <- m > rownames (m.names) <- letters [1:3] > dfm.names <- data.frame (c = 1 : 3, m = I (m.names)) > str (dfm.names) > > rbind (m, m.names) > rbind (m.names, m) > rbind (dfm, dfm.names) > > #not working > rbind (dfm.names, dfm) > > Error in rbind(deparse.level, ...) : replacement has length zero > > rbind (dfm, dfm.names)$m > > > [,1] [,2] [,3] [,4] > > <NA> 1 4 7 10 > > <NA> 2 5 8 11 > > <NA> 3 6 9 12 > > a 1 4 7 10 > > b 2 5 8 11 > > c 3 6 9 12 > > > > ________________________________ > > Important: This email is confidential and may be privileged. If you are not the intended recipient, please delete it and notify us immediately; you should not copy or use it for any purpose, nor disclose its contents to any other person. Thank you. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2016 | www.rinfinance.com
With thanks to Krzysztof Banas (and Joshua Ulrich), Almost 2.5 years ago, there's been a bug report that never made it to https://bugs.r-project.org/ and has been forgotten ... until I've stumbled over this again. I've found an easy fix (and improved readability also by adding comments) and committed the fix to R-devel rev 76612 , planned to be ported to R 3.6.0 patched a bit later. Thank you once more! Martin -- Martin Maechler ETH Zurich and R Core Team>>>>> Joshua Ulrich >>>>> on Sat, 21 Jan 2017 11:58:18 -0600 writes:> I'm not sure whether or not this is a bug, but I did isolate the line > where the error is thrown: > src/library/base/R/dataframe.R:1395. > https://github.com/wch/r-source/blob/01374c3c367fa12f555fd354f735a6e16e5bd98e/src/library/base/R/dataframe.R#L1395 > The error is thrown because the line attempts to set a subset of the > rownames to NULL, which fails. R> options(error = recover) R> rbind(dfm.names, dfm) > Error in rownames(value[[jj]])[ri] <- rownames(xij) : > replacement has length zero > Enter a frame number, or 0 to exit > 1: rbind(dfm.names, dfm) > 2: rbind(deparse.level, ...) > Selection: 2 > Called from: top level > Browse[1]> rownames(value[[jj]]) > [1] "a" "b" "c" NA NA NA > Browse[1]> rownames(xij) > NULL > Browse[1]> ri > [1] 4 5 6 > Browse[1]> rownames(value[[jj]])[ri] > [1] NA NA NA > On Mon, Jan 16, 2017 at 7:50 PM, Krzysztof Banas <krzysztof at nus.edu.sg> wrote: >> I suspect there may be a bug in base::rbind.data.frame >> >> Below there is minimal example of the problem: >> >> m <- matrix (1:12, 3) >> dfm <- data.frame (c = 1 : 3, m = I (m)) >> str (dfm) >> >> m.names <- m >> rownames (m.names) <- letters [1:3] >> dfm.names <- data.frame (c = 1 : 3, m = I (m.names)) >> str (dfm.names) >> >> rbind (m, m.names) >> rbind (m.names, m) >> rbind (dfm, dfm.names) >> >> #not working >> rbind (dfm.names, dfm) >> >> Error in rbind(deparse.level, ...) : replacement has length zero >> >> rbind (dfm, dfm.names)$m >> >> >> [,1] [,2] [,3] [,4] >> >> <NA> 1 4 7 10 >> >> <NA> 2 5 8 11 >> >> <NA> 3 6 9 12 >> >> a 1 4 7 10 >> >> b 2 5 8 11 >> >> c 3 6 9 12 >> >> >> >> ________________________________ >> >> Important: This email is confidential and may be privileged. If you are not the intended recipient, please delete it and notify us immediately; you should not copy or use it for any purpose, nor disclose its contents to any other person. Thank you. >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel > -- > Joshua Ulrich | about.me/joshuaulrich > FOSS Trading | www.fosstrading.com > R/Finance 2016 | www.rinfinance.com > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Reasonably Related Threads
- bug in rbind?
- rbind has confusing result for custom sub-class (possible bug?)
- rbind has confusing result for custom sub-class (possible bug?)
- rbind on data.frame that contains a column that is also a data.frame
- rbind has confusing result for custom sub-class (possible bug?)