Select multiple ranges of columns using column names in data.table

后端 未结 2 1634
花落未央
花落未央 2021-01-25 14:10

Let say I have a data table,

dt = data.table(matrix(1:50, nrow = 5));
colnames(dt) = letters[1:10];

> dt
   a  b  c  d  e  f  g  h  i  j
1: 1  6 11 16 21 26          


        
2条回答
  •  一生所求
    2021-01-25 14:21

    We can use the range part in .SDcols and then append the other column by concatenating

    dt[, c(list(a= a), .SD) , .SDcols = c:d]
    

    If there are multiple ranges, we create a sequence of ranges by match, and then get the corresponding column names

    i1 <- match(c("c", "f"), names(dt))
    j1 <- match(c("d", "h"), names(dt))
    nm1 <- c("a", names(dt)[unlist(Map(`:`, i1, j1))], "j")
    dt[, ..nm1]
    #   a  c  d  f  g  h  j
    #1: 1 11 16 26 31 36 46
    #2: 2 12 17 27 32 37 47
    #3: 3 13 18 28 33 38 48
    #4: 4 14 19 29 34 39 49
    #5: 5 15 20 30 35 40 50
    

    Also, the dplyr methods can be used within the data.table

    dt[, select(.SD, a, c:d, f:h, j)]
    #   a  c  d  f  g  h  j
    #1: 1 11 16 26 31 36 46
    #2: 2 12 17 27 32 37 47
    #3: 3 13 18 28 33 38 48
    #4: 4 14 19 29 34 39 49
    #5: 5 15 20 30 35 40 50
    

提交回复
热议问题