rdata.tabler-factor

Change factor levels in data.tables by name


I understand how I can change a column's levels when I explicitly define the column name like in this example from Matt Dowle :

setattr(mydt$value,"levels",c(...))

Though, in real-life problems, you often don't know the column names. This technique does not seem to work when "value" is not explicitly given in the code :

setattr(mydt[,get("value")],"levels",c(...))

And that is a problem. I guess this syntax gives me the column by copy and not a reference to the column, contrary to mydt$value, but I don't know how to fix this :'(

Thanks

Please see this older question


Solution

  • This would work:

    setattr(mydt[["value"]],"levels",c(...))