Why doesn't setDT have any effect in this case?

前端 未结 1 773
遇见更好的自我
遇见更好的自我 2021-01-19 03:43

Consider the following code

library(data.table) # 1.9.2
x <- data.frame(letters[1:2]) 
setDT(x) 
class(x)
## [1] \"data.table\" \"data.frame\"
         


        
1条回答
  •  终归单人心
    2021-01-19 04:32

    setDT changes the data.frame and returns it invisibly. Since you don't save this data.frame, it is lost. All you need to do is somehow save the data.frame, so that the data.table is also saved. E.g.

    setDT(y <- data.frame(x)) 
    class(y)
    ## [1] "data.table" "data.frame"
    

    or

    z <- setDT(data.frame(x))
    class(z)
    ## [1] "data.table" "data.frame"
    

    0 讨论(0)
提交回复
热议问题