基于列中最常见的值设置子集数据

时间:2017-06-14 14:06:43

标签: r subset

我有一个如下所示的数据集:

head(data1)
  Data number PatientSID
1           1   24663193
2           3    7451277
3           6    7449440
4           8    7350669
5           9    7328477
6          11    7324432

                Condition                                                                                                                                                                                                                                                                                            
1 acute coronary syndrome
2          abdominal pain
3               epistaxis
4                leg pain
5       chronic back pain
6               back pain

我使用聚合函数来查看患者的频率条件:

x <- aggregate(data.frame(count = data1$Condition), list(value = data1$Condition), length)
head(x,10)
                       value count
1                          3   108
2         4 wheeler accident     1
3                  abdominal     1
4         abdominal aneurysm     1
5  abdominal aortic aneurysm     1
6         abdominal bloating     2
7           abdominal cramps     2
8       abdominal discomfort     6
9       abdominal distension     2
10      abdominal distention    21

现在基于上面的输出,我想将data1子集化为仅包含条件计数&gt; = 10的行的数据帧。因此,我的子集将包含具有条件“3”和“腹胀”的所有行。 我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

您可以使用dplyr

x.sub <- x %>%
         filter(count >= 10)

data1.sub <- data1[data1$Condition %in% x.sub$value, ]