Taking a data.table slice with a sequence of (row,col) indices

前端 未结 2 819
囚心锁ツ
囚心锁ツ 2021-01-25 11:43

I have a data.table that resembles the one below.

tab <- data.table(a = c(NA, 42190, NA), b = c(42190, 42190, NA), c = c(40570, 42190, NA))
tab
          


        
2条回答
  •  不知归路
    2021-01-25 11:58

    There is a faster way to do this than coercing to either matrix or data.frame. Just use the [data.frame function.

    `[.data.frame`( tab,  cbind(ri,ci) )
    [1]    NA 42190    NA
    

    This is the functional syntax for the [.data.frame function.

提交回复
热议问题