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

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

问题描述

DT-带有列 A(列索引== 1), B(列索引2), C等的data.table

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??

例如,下一个代码不起作用:

for example, code like next not works :

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


推荐答案

我们可以使用获取行索引。我并使用该索引来子集 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



数据



data

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

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

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