Select column of data.table and return vector

前端 未结 2 400
一整个雨季
一整个雨季 2021-01-24 19:38

Is it possible to select a column of a data.table and get back a vector? In base R, the argument drop=TRUE would do the trick. For example,

library(         


        
相关标签:
2条回答
  • 2021-01-24 20:29

    With data.frame, the default is drop = TRUE and in data.table, it is the opposite while it is done internally. According to ?data.table

    drop - Never used by data.table. Do not use. It needs to be here because data.table inherits from data.frame.

    In order to get the same behavior, we can use [[ to extract the column by passing a string

    identical(dat[["Species"]], iris[, "Species"])
    #[1] TRUE
    

    Or

    dat$Species
    

    By using [[ or $, it extracts as a vector while also bypass the data.table overhead

    0 讨论(0)
  • 2021-01-24 20:36

    See data.table FAQ #1.1. This comes as a feature since 2006.

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