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

前端 未结 2 834
醉话见心
醉话见心 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)
    
    0 讨论(0)
  • 2021-01-05 19:20

    @akrun's answer gives you the correct alternative. If you want to know why you need it, here's the more detailed explanation:

    The way the data.table subset operation works, in most cases the j expression in dt[i, j, by] with no i or by, is evaluated in the frame of the data table, and returned as is, whether or not it has anything to do with the data table outside the brackets. In versions earlier than 1.9.8, your first code snippet: dt[,c(4, 5, 6)] evaluates to the numeric vector c(4, 5, 6), not the 4th, 5th, and 6th columns. This changed as of data.table v1.9.8 (released November 2016) ( scroll down to v.1.9.8 potentially breaking changes), because people, unsurprisingly, expected dt[,c(4, 5, 6)] to give the 4th 5th and 6th columns. Now, if the j expression is the variable names or numbers, with is automatically set to FALSE. This effectively produces behavior similar to subsetting a data frame (not exactly the same, but similar).

    So your second code snippet (where dt[, a] evaluates to a, rather than uses a to subset the columns) is actually the default, and the first is a special case.

    To illustrate the odd but standard behavior here, try:

    dt[, diag(5)]
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    1    0    0    0    0
    # [2,]    0    1    0    0    0
    # [3,]    0    0    1    0    0
    # [4,]    0    0    0    1    0
    # [5,]    0    0    0    0    1
    

    No matter what your dt is, so long as it is a data.table, it will evaluate to the 5*5 identity matrix

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