6

I have a function that returns a data.table with various useful user-defined attributes attached. I notice, though, that the attributes disappear when one manipulates 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's there
attributes(my_dt)
# but here it's gone
attributes(my_dt[order(col1)]) 

Is there any way to make attributes of a data.table 'persist' for cases like the above (besides just storing them in a separate object)?

It seems attributes do 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), ]) 
smci
  • 32,567
  • 20
  • 113
  • 146
arvi1000
  • 9,393
  • 2
  • 42
  • 52
  • 8
    Not currently supported. Open feature request can be found in [data.table#995](https://github.com/Rdatatable/data.table/issues/995). – jangorecki Dec 16 '15 at 17:38
  • 1
    Ah, thanks. Did not find any earlier discussion of this. I'll leave the question here though since you've linked to it from github as evidence of user appetite for this feature. – arvi1000 Dec 16 '15 at 17:51

1 Answers1

1

Feature has been added to 1.12.0 when subset was made parallel by Matt. So attributes are now being retained.

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

attr(my_dt, 'title') <- 'This is my data.table'
attr(my_dt, 'title')
#[1] "This is my data.table"
attr(my_dt[order(col1)], 'title')
#[1] "This is my data.table"
jangorecki
  • 16,384
  • 4
  • 79
  • 160