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

后端 未结 2 1636
花落未央
花落未央 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:35

    Here is a workaround with cbind and two or more selections.

    cbind(dt[, .(a)], dt[, c:d])
    #    a  c  d
    # 1: 1 11 16
    # 2: 2 12 17
    # 3: 3 13 18
    # 4: 4 14 19
    # 5: 5 15 20
    

提交回复
热议问题