根据条件语句从数据框中删除行?

时间:2017-05-01 17:39:16

标签: r

我有一个事故的数据框(称之为df)。每次事故都有一个与之相关的#,每个人参与的#和事故类型。它看起来像这样:

x               y                    z
accident #1   person A    accident type #1
accident #1   person A    accident type #2
accident #2   person A    accident type #1
accident #2   person B    accident type #2
accident #2   person B    accident type #3
accident #3   person C    accident type #1

在上述案件中,A人参与了两起事故。在第一次事故中,有两种类型的' A人参与的事故。 B人与A人有关,但只涉及一起事故,有两种事故类型。 C人也只参与了一起事故。

我想收集仅参与一次事故的人员。但是,我想要包括他们所有的事故类型。所以使用上面的例子,我想要这个:

x               y                    z
accident #2   person #2    accident type #2
accident #2   person #2    accident type #3
accident #3   person #3    accident type #1

我怎么能在R中做到这一点?

2 个答案:

答案 0 :(得分:3)

您可以使用group_by,使用filtern_distinctlibrary(dplyr) df %>% group_by(y) %>% filter(n_distinct(x) == 1) %>% ungroup() 执行此操作:

:- use_module(library(clpfd)).

from_to(X-Y,X1-Y1):-
   X1 #= X+1,
   Y1 #= Y-2.
from_to(X-Y,X1-Y1):-
   X1 #= X+2,
   Y1 #= Y-1.

答案 1 :(得分:0)

我们可以使用data.table

library(data.table)
setcolorder(setDT(df)[, .SD[uniqueN(x)==1] , y], names(df))[]
#            x        y                z
#1: accident #2 person B accident type #2
#2: accident #2 person B accident type #3
#3: accident #3 person C accident type #1