4

假设我想在一列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" 
4

1 回答 1

4
DT[,N:=as.myclass(N)]

应该管用

于 2013-02-07T10:19:41.657 回答