Suppose I want to set the class on all the items in a data.table column. I feel like there should be a way to do this sort of thing from inside the DT[,j] part, but it doesn’t seem to work. I have to do it using $. Does anyone know why?
> DT <- data.table(L = letters, N = 1:26)
> sapply(DT, class)
L N
"character" "integer"
> DT[, {class(N) <- "MyClass"}] # Doesn't work
[1] "MyClass"
> sapply(DT, class)
L N
"character" "integer"
> DT[, class(N) <- "MyClass"] # Doesn't work
[1] "MyClass"
> sapply(DT, class)
L N
"character" "integer"
> class(DT$N) <- "MyClass" # Works
> sapply(DT, class)
L N
"character" "MyClass"
Should work