0

Is it possible to add metainfo to each row within a data.table and also filter by such? And if so, how can I do it?

Let's say for simplicity that I have a data table with 2 columns, A and B, where B contains numeric data. I need to be able to mark (some, all, just one, etc.) rows as FIX, do math operations, joins and so on, and later mark them as UNFIX so that when performing such operations, results are not affected by the metainfo as might be the case if they were just another column instead of a hidden attribute.

Leonardo Lanchas
  • 1,616
  • 1
  • 15
  • 37
  • 1
    I don't understand your argument for not making them proper columns. Can you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make it more clear? – MrFlick May 08 '17 at 15:34
  • 1
    you can use a separate vector if you want. e.g. `fix.rows = c(T,F,F)` or `fix.rows = c(1, 17, 23)`. Then do data table operations using this: `dt[fix.rows, V1 := x]`. But I don't see anything that couln't also be achieved simply using an extra column – dww May 08 '17 at 15:34
  • @MrFlick, For example, I have a set functions that perform join operations between 2 data tables using intersect, given that the column number may grow in the future: sk = intersect(names(dt1), names(dt2)) sk = sk[ !sk %in% "VALUE"] dt1[dt2, VALUE := VALUE + i.VALUE, on = sk] – Leonardo Lanchas May 08 '17 at 15:39

1 Answers1

0

R supports attributes on all objects. So you can assign attributes to your list although it may not be the best approach - creating and dropping a new column is likely the best solution. I would note that if you preform any operations on your list you will lose the attributes unless you copy them to the new object. Here is a simple example:

df <- data.frame(A = c(1, 2, 3),
                 B = c(1, 2, 3))

attr(df, "isFixed") <- c("unfixed","unfixed","unfixed")

attributes(df)$isFixed

attributes(df)$isFixed[1] <- "fixed"

attributes(df)$isFixed   
Ian Wesley
  • 3,565
  • 15
  • 34