Access data.table columns with strings

后端 未结 3 1111
予麋鹿
予麋鹿 2020-12-15 10:46

Apologies for a question that probably makes it obvious that I usually work in Python/pandas, but I\'m stuck with this. How do I select a data.table column usin

3条回答
  •  时光说笑
    2020-12-15 11:30

    You can use get() as the j argument using single brackets:

    library(data.table)
    dt <- data.table(iris)
    dt[, get("Species")]
    

    The result:

    [1] setosa     setosa     setosa     setosa     setosa     setosa .....
    

    You can also use a string directly inside the double bracket operator, like this:

    dt[["Species"]]
    

提交回复
热议问题