r 中的 data.table :使用列索引的子集 [英] data.table in r : subset using column index

查看:17
本文介绍了r 中的 data.table :使用列索引的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DT - data.table 包含列A"(列索引==1)、B"(列索引 2)、C"等

DT - data.table with column "A"(column index==1), "B"(column index 2), "C" and etc

例如,下一个代码生成子集 DT1,其中包含 A==2 的行:

for example next code makes subset DT1 which consists rows where A==2:

DT1 <- DT[A==2, ]

但是如何只使用列索引来制作像 DT1 这样的子集??

BUT How can I make subsets like DT1 using only column index??

例如,类似 next 的代码不起作用:

for example, code like next not works :

DT1 <- DT[.SD==2, .SDcols = 1]

推荐答案

我们可以用 .I 获取行索引,并用它来子集 DT

We can get the row index with .I and use that to subset the DT

DT[DT[, .I[.SD==2], .SDcols = 1]]
#   A B C
#1: 2 3 4

数据

DT <- data.table(A = 1:5, B = 2:6, C = 3:7)

这篇关于r 中的 data.table :使用列索引的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆