Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User-specified attributes of data.table get removed

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), ]) 
like image 805
arvi1000 Avatar asked Dec 16 '15 17:12

arvi1000


People also ask

What does the data table () function provide to big data processing?

It offers fast and memory efficient: file reader and writer, aggregations, updates, equi, non-equi, rolling, range and interval joins, in a short and flexible syntax, for faster development. It is inspired by A[B] syntax in R where A is a matrix and B is a 2-column matrix. Since a data. table is a data.

How do I add a column to a data table in R?

A column can be added to an existing data table using := operator. Here ':' represents the fixed values and '=' represents the assignment of values. So, they together represent the assignment of fixed values. Therefore, with the help of “:=” we will add 2 columns in the above table.

Which library is data table in R?

Data. table is an extension of data. frame package in R. It is widely used for fast aggregation of large datasets, low latency add/update/remove of columns, quicker ordered joins, and a fast file reader.


1 Answers

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"
like image 98
jangorecki Avatar answered Sep 29 '22 05:09

jangorecki