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

Davis Vaughan d@v|@ @end|ng |rom r@tud|o@com
Sun Feb 14 15:50:35 CET 2021


Hi all,

I believe that the internal row names object created at this line in
`row_names_gets()` should be using `-n`, not `n`.
https://github.com/wch/r-source/blob/b30641d3f58703bbeafee101f983b6b263b7f27d/src/main/attrib.c#L71

This can currently generate corrupt internal row names when using
`attributes<-` or `structure()`, which calls `attributes<-`.

# internal row names are typically `c(NA, -n)`
df <- data.frame(x = 1:3)
.row_names_info(df, type = 0L)
#> [1] NA -3

# using `attributes()` materializes their non-internal form
attrs <- attributes(df)
attrs
#> $names
#> [1] "x"
#>
#> $class
#> [1] "data.frame"
#>
#> $row.names
#> [1] 1 2 3

# let's make a data frame from scratch with `attributes<-`
data <- list(x = 1:3)
attributes(data) <- attrs

# oh no!
.row_names_info(data, type = 0L)
#> [1] NA  3

# Note: Must have `nrow(df) > 2` to demonstrate this bug, as otherwise
# internal row names are not attempted to be created in the C level
# `row_names_gets()`

Thanks,
Davis

	[[alternative HTML version deleted]]



More information about the R-devel mailing list