假设我想在一列class
中的所有项目上设置。data.table
我觉得应该有一种方法可以从DT[,j]
零件内部做这种事情,但它似乎不起作用。我必须使用$
. 有谁知道为什么?
> 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"