我有一个函数返回data.table
,附加了各种有用的用户定义属性。但是,我注意到,当操纵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)])
有没有办法让data.table'persist'的属性适用于上述情况(除了将它们存储在一个单独的对象中)?
似乎属性确实存在于常规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), ])
答案 0 :(得分:1)
当子集由Matt并行处理时,功能已添加到1.12.0。因此,现在保留了属性。
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"