子集data.table按列索引来自另一个对象(大小为1的数字向量)

时间:2018-09-05 12:50:33

标签: r data.table subset

我正在尝试根据参数l(行号)和cn(列号)number本身来对data.table进行子集化。基于l的子集似乎有效。但是在cn(或get(cn))上进行子集设置将永远无法进行。

例如:

> dt <- iris %>% data.table()
> 
> #Subseting (works)
> dt[10,3]
   Petal.Length
1:          1.5
> 
> #Subseting by exteral parameter (object)
> cn <- 3 # Petal.Length
> l <- 10
> dt[l,3] #works
   Petal.Length
1:          1.5
> dt[10,cn]
Error in `[.data.table`(dt, 10, cn) : 
  j (the 2nd argument inside [...]) is a single symbol but column name 'cn' is not found. Perhaps you intended DT[, ..c]. This difference to data.frame is deliberate and explained in FAQ 1.1.
> dt[l,cn]
Error in `[.data.table`(dt, l, cn) : 
  j (the 2nd argument inside [...]) is a single symbol but column name 'cn' is not found. Perhaps you intended DT[, ..c]. This difference to data.frame is deliberate and explained in FAQ 1.1.
> dt[l,get(cn)]
Error in get(cn) : invalid first argument

是否可以对来自外部对象的列号进行子集化?

0 个答案:

没有答案