r - 数据表 DT[i, j, by] 不使用 i 中的条件选择组,试图过滤组内唯一值的数量

标签 r select filter data.table unique

我不确定我的数据在这里发生了什么,也许我遗漏了一些简单的东西。

我的数据集有一个 id 'dSc' 和一个分配给每个 id 的集群(点)。我想过滤掉那些只分配了一个集群的 ID,

例如BS:100021 仅分配了 1 个聚类点,而 BS:100135 分配了 6 个聚类点,来自以下示例:

set.seed(1) 
xx  <- cbind(rep("BS:100021",30),rep(1,30))
yy  <- cbind(rep("BS:100135",60),rep(1:6,10))
mm  <- as.data.table(rbind(xx,yy))
names(mm) <- c("dSc","Cluster")

但如果我想过滤掉 "BS:100021" n 相似的,我正在尝试

mm[length(unique(Cluster)) > 1,.SD,dSc]

我还在努力

          dSc Cluster
 1: BS:100021       1
 2: BS:100021       1
 3: BS:100021       1
 4: BS:100021       1
 5: BS:100021       1
 6: BS:100021       1
 7: BS:100021       1
 8: BS:100021       1
 9: BS:100021       1
10: BS:100021       1
11: BS:100021       1
12: BS:100021       1
13: BS:100021       1
14: BS:100021       1
15: BS:100021       1
16: BS:100021       1
17: BS:100021       1
18: BS:100021       1
19: BS:100021       1
20: BS:100021       1
21: BS:100021       1
22: BS:100021       1
23: BS:100021       1
24: BS:100021       1
25: BS:100021       1
26: BS:100021       1
27: BS:100021       1
28: BS:100021       1
29: BS:100021       1
30: BS:100021       1
31: BS:100135       1
32: BS:100135       2
33: BS:100135       3
34: BS:100135       4
35: BS:100135       5
36: BS:100135       6
37: BS:100135       1
38: BS:100135       2
39: BS:100135       3
40: BS:100135       4
41: BS:100135       5
42: BS:100135       6
43: BS:100135       1
44: BS:100135       2
45: BS:100135       3
46: BS:100135       4
47: BS:100135       5
48: BS:100135       6
49: BS:100135       1
50: BS:100135       2
51: BS:100135       3
52: BS:100135       4
53: BS:100135       5
54: BS:100135       6
55: BS:100135       1
56: BS:100135       2
57: BS:100135       3
58: BS:100135       4
59: BS:100135       5
60: BS:100135       6
61: BS:100135       1
62: BS:100135       2
63: BS:100135       3
64: BS:100135       4
65: BS:100135       5
66: BS:100135       6
67: BS:100135       1
68: BS:100135       2
69: BS:100135       3
70: BS:100135       4
71: BS:100135       5
72: BS:100135       6
73: BS:100135       1
74: BS:100135       2
75: BS:100135       3
76: BS:100135       4
77: BS:100135       5
78: BS:100135       6
79: BS:100135       1
80: BS:100135       2
81: BS:100135       3
82: BS:100135       4
83: BS:100135       5
84: BS:100135       6
85: BS:100135       1
86: BS:100135       2
87: BS:100135       3
88: BS:100135       4
89: BS:100135       5
90: BS:100135       6
          dSc Cluster

最佳答案

你可以使用:

list.of.dSc <- NULL

for(i in seq_along(unique(mm$dSc))){
    if(length(unique(mm[mm$dSc == as.character(unique(mm$dSc)[i]), "Cluster"])) == 1){
        list.of.dSc <- c(list.of.dSc, unique(as.character(mm$dSc))[i])
       }
}

mm[!(mm$dSc %in% list.of.dSc),]

         dSc Cluster
31 BS:100135       1
32 BS:100135       2
33 BS:100135       3
34 BS:100135       4
35 BS:100135       5
...

你找到只有一个集群类的所有实例(或每个 dSc),将它们添加到列表(list.of.dSc),然后使用 %in% 过滤 mm 列表中的所有条目。

它不是很漂亮,但我认为它解决了你的问题。

关于r - 数据表 DT[i, j, by] 不使用 i 中的条件选择组,试图过滤组内唯一值的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36292702/

相关文章:

github - 如何过滤合并并关闭但分支尚未删除的Github PR

Java 从集合中获取多个项目

mysql - 从表中选择消息

r - 如何 pivot_longer 一组多列?以及如何从那种长格式返回到原来的宽格式?

r - LIKE sqldf 上的内连接

R——从函数 "polyroot"() 和 "Im()"中查找非复杂解

css - Materialise 的 select 底线无法禁用

表中所选记录的一列上的 MySQL 时间差

javascript - 为什么 ngModel 不可分配

r - 迭代 rvest 抓取函数给出 : "Error in open.connection(x, "rb") : Timeout was reached"