How to extract columns from data table by indices in R?

前端 未结 2 860
醉话见心
醉话见心 2021-01-05 18:33

I want to extract the 4th, 5th, and 6th column from a data table named dt

the following method works:

    dt[, c(4,5,6)]

but the fo

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-05 19:20

    We can use double dots (..) before the object 'a' to extract the columns

    dt[, ..a]
    #   col4 col5 col6
    #1:    4    5    6
    #2:    5    6    7
    #3:    6    7    8
    #4:    7    8    9
    

    Or another option is with = FALSE

    dt[, a, with = FALSE]
    

    data

    dt <- data.table(col1 = 1:4, col2 = 2:5, col3 = 3:6, col4 = 4:7, col5 = 5:8, col6 = 6:9)
    

提交回复
热议问题