Custom data.table attributes are removed

I have a function that returns data.tablewith various useful user attributes. However, I notice that the attributes disappear when they manipulate the data.table.

library(data.table)
my_dt <- data.table(col1 = rnorm(20), col2 = letters[1:20])

# store some user attribute
attr(my_dt, 'title') <- 'This is my data.table'
# now it there
attributes(my_dt)
# but here it gone
attributes(my_dt[order(col1)]) 

Is there a way to make data.table 'persist' attributes for cases like the above (except that they are stored in a separate object)?

Attributes seem to persist for regular data.frames

my_df <- data.frame(col1 = rnorm(20), col2 = letters[1:20])

# store some user attribute
attr(my_df, 'title') <- 'This is my data.frame'
# there it is
attributes(my_df) 
# still there
attributes(my_df[order(my_df$col1), ]) 
+4
source share

Source: https://habr.com/ru/post/1620400/


All Articles