[Rd] Corrupt internal row names when creating a data.frame with `attributes<-`

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Mon Mar 1 20:13:37 CET 2021


>>>>> Davis Vaughan 
>>>>>     on Tue, 16 Feb 2021 14:50:33 -0500 writes:

    > This originally came up in this dplyr issue:
    > https://github.com/tidyverse/dplyr/issues/5745

    > Where `tibble::column_to_rownames()` failed because it
    > eventually checks `.row_names_info(.data) > 0L` to see if
    > there are automatic row names, which is in line with the
    > documentation that Kevin pointed out: "type = 1 the latter
    > with a negative sign for ‘automatic’ row names."

    > Davis


    > On Tue, Feb 16, 2021 at 2:29 PM Bill Dunlap
    > <williamwdunlap using gmail.com> wrote:

    >> as.matrix.data.frame does not take the absolute value of
    >> that number:

slightly changed and extended by MM {and as R script} :

------------------------------------------------------------------------

dPos <- structure(list(X=11:14, 1:4), class="data.frame", row.names=c(NA, +4L))
dNeg <- structure(list(X=11:14, 1:4), class="data.frame", row.names=c(NA, -4L))
##
all_rn_info <- function(x) lapply(setNames(,0:2),
                               function(tp) .row_names_info(x, type=tp))
str(all_rn_info(dPos))
## List of 3
##  $ 0: int [1:2] NA 4
##  $ 1: int 4
##  $ 2: int 4
str(all_rn_info(dNeg))
## List of 3
##  $ 0: int [1:2] NA -4
##  $ 1: int -4
##  $ 2: int 4
stopifnot(exprs = {
    identical(rownames(dPos), as.character(1:4))
    identical(rownames(dPos), rownames(dNeg))
    ## using as.matrix.data.frame() which differentiates, too :
    identical(rownames(as.matrix(dPos)), as.character(1:4))
    is.null  (rownames(as.matrix(dNeg)))
    ## BTW, also :
    identical(attributes(dPos), attributes(dNeg)) ## and hence also
    identical(dPos, dNeg) # another case where identical() is possibly too "tolerant"
})

## and for your interest, these *also* have both 'c(NA, +|n|)'  ==> give '+4'
.row_names_info(dInt1 <- structure(list(X=11:14, 1:4), class="data.frame", row.names=1:4))
.row_names_info(dInt2 <- local({ dd <- data.frame(X=11:14, 1:4, fix.empty.names = FALSE)
                                 attr(dd, "row.names") <- 1:4; dd }))
stopifnot(exprs = {
    identical(dInt1, dInt2)
    identical(all_rn_info(dInt1),
              all_rn_info(dInt2))
    identical(all_rn_info(dPos),
              all_rn_info(dInt1))
})

------------------------------------------------------------------------

There never was a conclusion here
(and the above is not the full context of the thread) .. 
but if I understand Bill and his example (extended above) correctly,
he's indirectly hinting toward that there is **no bug** here :

1) You can use structure() well to get "truly automatic" row
   names by setting the row.names correctly to  c(NA, -3L)
   {yes, which is  c(NA_integer_, -3L) }

2) There's a subtle difference between *two* kinds of automatic
   row names, on purpose, notably used in  as.matrix.data.frame():
   c(NA, +3)  are automatic row names, too, but which translate also to
   matrix row names hence are somewhat slightly less automatic ... 

   Note that you may see this documented by careful reading of
   the 'Note' in  help(row.names) *and* the 'Examples' section
   of that help page....

Last but not least:  We (R Core) did not document the nitty
gritty details here partly on purpose, because they should've
been subject to change, see e.g. the word "currently" in the
?row.names help page.

Notably with ALTREP objects, we could use "regular"  1:n
integer row names which would be ALTREP compacted automatically
for non-small 'n'.

Last but not least, the check in tibble that was mentioned in
this thread, should almost surely be fixed, if gives a problem
for these example, and I claim it has been *that* code that has
buggy rather than base R's one. 

Martin



More information about the R-devel mailing list