attr_DT | R Documentation |
This is a workaround with reference to https://github.com/Rdatatable/data.table/issues/995: retain attributes while chaining. Instead of storing attributes within the data.table object DT, they are stored and may be accessed via a separate data.table object DT.attributes that lives in the .GlobalEnv. The function allows to access attributes by name or assign values to them in a similar style like base::attr.
attr_DT(DT, j, value = NULL, delete = FALSE)
DT |
A |
j |
A column name to name the attribute that shall be accessed or associated with a value. |
value |
The value to be assigned to |
delete |
By default |
A data.table
is generated in the .GlobalEnv
named DT.attributes that stores the attributes.
myDT <- data.table(x = c(1:3), y = (letters[1:3])) my_attr1 <- data.table(A1 = c(9:10), A2 = c(letters[9:10])) my_attr2 <- "attribute2" attr_DT(myDT, j = "name_myattr1", value = my_attr1) attr_DT(myDT, j = "name_myattr2", value = my_attr2) myDT.attributes # name_myattr1 name_myattr2 # 1: <data.table> attribute2 attr_DT(myDT, j = "name_myattr1") # A1 A2 # 1: 9 i # 2: 10 j
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.