You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Question / Feature request.
Any tricky way to retain attributes when chaining? of course not all should be retained ("index","sorted","names", etc.).
library(data.table)
DT<- data.table(a= rep(1:5,2), b= rnorm(10))
setattr(DT,"my_custom_attr","attr_value")
str(DT)
# I would like to reuse attribute while chaining and also after chainingDT[,attr(.SD,"my_custom_attr")] # NULL
attr(DT[,lapply(.SD,sum),by="a"],"my_custom_attr") # NULL
So the Feature request would be about retaining all user defined (non-DT related: "sorted","index", etc.) attributes.
Or maybe retain attrs defined by name, something like
Guy, is there are any problems with a solution like the following?
First, adding a function that will copy required attributes:
retainAttributes<-function(x, ans) {
if ("retain.attributes"%in% names(attributes(x))) {
for (ain c(attr(x, "retain.attributes"), "retain.attributes")) {
attr(ans, a) <- attr(x, a)
}
}
ans
}
And then using it is as a wrapper when neccessary in [.data.table:
diff --git a/R/data.table.R b/R/data.table.R
index d18dd3f..dda4985 100644
--- a/R/data.table.R
+++ b/R/data.table.R
@@ -1343,7 +1343,7 @@ chmatch2 <- function(x, table, nomatch=NA_integer_) {
setattr(ans, "class", class(x)) # fix for #5296
setattr(ans, "row.names", .set_row_names(nrow(ans)))
- if (!with || missing(j)) return(alloc.col(ans))
+ if (!with || missing(j)) return(alloc.col(retainAttributes(x, ans)))
SDenv$.SDall = ans
SDenv$.SD = if (!length(othervars)) SDenv$.SDall else shallow(SDenv$.SDall, setdiff(ansvars, othervars))
I am not an expert with data.table or even with programming, therefore, I am currently using a clumsy function that stores custom attributes in a separate data.table called DT.attributes in the .GlobalEnv using eval(parse()) expressions, that makes accessing/updating these attributes similar to using attr. The code does not interfere with source code of data.table, hence, the object created is stable during chaining and can always be referenced/accessed by its name. The clear drawback is clearly that the functions messes around in .GlobalEnv, which is not a very good idea. Knowing that my solution is not clean and clumsy, I thought this might still be helpful to somebody and wanted to share this here. Please do not hang me for this attempt...
Activity
arunsrinivasan commentedon Jan 3, 2015
Have marked as FR for now.
jangorecki commentedon Oct 30, 2015
new SO about that: https://stackoverflow.com/questions/33441632/retaining-metadata-when-subsetting-in-data-table
this SO highlights it is a gap comparing to data.frame
edit: another new one: https://stackoverflow.com/questions/34318299/user-specified-attributes-of-data-table-get-removed
assaron commentedon Aug 13, 2016
Guy, is there are any problems with a solution like the following?
First, adding a function that will copy required attributes:
And then using it is as a wrapper when neccessary in
[.data.table:With these, the following scenario works:
I can't provide a full patch, as
[.data.tableis long and full of returns... I'm not 100% sure which ones should be modified.jangorecki commentedon Aug 13, 2016
Interesting idea, but
attr<-isn't best idea. There is in-memory copy there, we usesetattrin such cases.assaron commentedon Aug 13, 2016
@jangorecki Could you explain a little bit more? I have the same address printed there:
What's get copied there?
assaron commentedon Aug 13, 2016
But it's just for my education, I have no problems with changing
attr<-tosetattr, or whatever else works best.jangorecki commentedon Aug 13, 2016
That is strange, when I was calling that before I get different address and now I'm getting the same. I must have made some wrong then.
changcw83 commentedon Feb 21, 2017
want this feature very much, it's not available yet?
dselivanov commentedon May 17, 2017
+1 for this features. Any concerns about solution above?
manuelbickel commentedon Mar 6, 2018
+1 for this feature also from my side.
I am not an expert with
data.tableor even with programming, therefore, I am currently using a clumsy function that stores custom attributes in a separatedata.tablecalledDT.attributesin the.GlobalEnvusingeval(parse())expressions, that makes accessing/updating these attributes similar to usingattr. The code does not interfere with source code ofdata.table, hence, the object created is stable during chaining and can always be referenced/accessed by its name. The clear drawback is clearly that the functions messes around in.GlobalEnv, which is not a very good idea. Knowing that my solution is not clean and clumsy, I thought this might still be helpful to somebody and wanted to share this here. Please do not hang me for this attempt...