-
Notifications
You must be signed in to change notification settings - Fork 991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I had expected rbindlist() to preserve attributes, but it doesn't. It'd be a nice feature; or an important limitation to disclose in the documentation. #5569
Comments
To be fair the x <- data.frame(a=0)
attr(x, "xa") <- 1
y <- data.frame(a=1)
attr(y, "ya") <- 1
z <- do.call(rbind, list(x,y))
attributes(z)
#> $names
#> [1] "a"
#>
#> $row.names
#> [1] 1 2
#>
#> $xa
#> [1] 1
#>
#> $class
#> [1] "data.frame" Neither does x <- data.frame(a=0)
y <- data.frame(a=1)
attr(x, "xa") <- 1
attr(y, "ya") <- 1
attributes(dplyr::bind_rows(x, y))
#> $names
#> [1] "a"
#>
#> $class
#> [1] "data.frame"
#>
#> $row.names
#> [1] 1 2
#>
#> $xa
#> [1] 1 |
Indeed, I'd be happy if My use-case for these methods is to add a row of experimental data to an existing table. I now realise that this is much more easily done with xdf <- data.frame(data=c(0))
attr(xdf, "xa") <- 0
xdf <- dplyr::bind_rows(xdf, c(data=1))
# I expect attributes(xdf) to include "xa". (PASS)
attributes(xdf)
#> $names
#> [1] "data"
#>
#> $class
#> [1] "data.frame"
#>
#> $row.names
#> [1] 1 2
#>
#> $xa
#> [1] 0
xdf <- data.frame(data=c(0))
attr(xdf, "xa") <- 0
xdf <- rbind(xdf, c(data=1))
# I expect attributes(xdf) to include "xa". (PASS)
attributes(xdf)
#> $names
#> [1] "data"
#>
#> $row.names
#> [1] 1 2
#>
#> $xa
#> [1] 0
#>
#> $class
#> [1] "data.frame"
library(data.table)
xdt <- data.table(data=0)
setattr(xdt, "xa", 0)
xdt <- dplyr::bind_rows(xdt, c(data=1))
# I expect class(xdt) to have "data.table" as its first element (PASS)
class(xdt)
#> [1] "data.table" "data.frame"
# I expect xdt to have an attribute "xa" (PASS)
attributes(xdt)
#> $names
#> [1] "data"
#>
#> $row.names
#> [1] 1 2
#>
#> $class
#> [1] "data.table" "data.frame"
#>
#> $.internal.selfref
#> <pointer: 0x000002c36fbc4bb0>
#>
#> $xa
#> [1] 0
# I expect class(xdt) to have a valid .internal.selfref structure. (FAIL)
data.table:::selfrefok(xdt)
#> [1] 0
library(data.table)
xdt <- data.table(data=0)
setattr(xdt, "xa", 0)
xdt <- rbindlist(list(xdt, data.table(data=1)))
# I expect class(xdt) to have a valid .internal.selfref structure. (PASS)
data.table:::selfrefok(xdt)
#> [1] 1
# I expect attributes(xdt) to include "xa". (FAIL)
attributes(xdt)
#> $names
#> [1] "data"
#>
#> $row.names
#> [1] 1 2
#>
#> $class
#> [1] "data.table" "data.frame"
#>
#> $.internal.selfref
#> <pointer: 0x000002c36fbc4bb0> I'll leave it to the DT team to decide whether to continue treating this as a feature-request, or (as I'd now suggest) as a request to amend the documentation of rbindlist(). A sentence would suffice, I should think, to inform users that -- unlike Your prompt response to my feature-request is very impressive, thanks! I'm extremely grateful for the bazillions of hours of volunteer labour, over the decades, which is making my current coding experience in R so so so much more pleasant and productive than my only prior experience with stochastic experimentation -- which was way back in the early 1990s, using S (yeah I'm an old codger! ;-) |
Preserve attributes with This is something that will definitely break a |
Yeah, it'd be a big shift in semantics of So... maybe... it'd make sense to define a new method for DTs which (essentially) overloads |
#
Minimal reproducible example
Created on 2022-12-20 with reprex v2.0.2
#
Output of sessionInfo()
Created on 2022-12-20 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: