r - data.table 的用户指定属性被删除

标签 r attributes data.table persistent user-defined

我有一个返回 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 的属性“持久化”(除了将它们存储在单独的对象中)?

似乎属性确实适用于常规 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), ]) 

最佳答案

当 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"

关于r - data.table 的用户指定属性被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34318299/

相关文章:

r - 对 R 中的嵌套 for 循环进行向量化(用于多类逻辑损失计算)

r - 用带孔的多边形制作 sf 对象并设置 crs

spring - 在没有类属性的xml中定义bean

c# - 在类级别获取描述属性

r - 查询: Why does base R behave this way with factor()?

asp.net-mvc-3 - MVC 3 中忽略 OutputCache 属性

r - data.table 的包装函数

基于另一列值的列中的随机数

R - 查找 data.table 中第一个非零元素的索引

r - 在 R : labelling points and scale 中使用 ggmap 和雄蕊图进行映射