如何在R数据表中的特定列之后选择组后的行的子集 [英] How do I select a subset of rows after group by a specific column in R Data table
本文介绍了如何在R数据表中的特定列之后选择组后的行的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据R数据表中特定列分组后的条件来选择行的子集。
以Mtcars数据为例。
I want to select a subset of rows based upon a condition after grouping by a specific column in R data table . Take Mtcars data for an example .
dt_mtcars <- as.data.table(mtcars)
dt_mtcars[,.N,by=.(hp)]
hp N
1: 110 3
2: 93 1
3: 175 3
4: 105 1
5: 245 2
6: 62 1
7: 95 1
8: 123 2
9: 180 3
10: 205 1
11: 215 1
12: 230 1
13: 66 2
14: 52 1
15: 65 1
16: 97 1
17: 150 2
18: 91 1
19: 113 1
20: 264 1
21: 335 1
22: 109 1
hp N
就像选择一个子集合的数据基于计数为eg在这里我想有所有的数据在dt_mtcars其中N> 1
如果我尝试这样的东西(这在概念上是我想要实现的)它显示所有行,而不是获得N大于1的行。
Now what I am trying to achieve is like selecting a sub set of data based on the count for e.g in here I want to have all the data in dt_mtcars where N >1 if I try something like this (which is conceptually what I am trying to achieve) it shows all the rows rather than getting the rows where N is greater than 1 .
dt_mtcars[.N>1,.SD,by=.(hp)]
请协助。
推荐答案
我们可以使用如果
dt_mtcars[, if(.N>1) .SD ,hp]
这篇关于如何在R数据表中的特定列之后选择组后的行的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文