我有一个像这样的数据框,我想把新系统和旧系统上的测试行数不相等的行去掉。
Person Test
1 new
1 new
1 old
1 old
2 new
2 new
2 old
我想删除新系统和旧系统中测试次数不相等的行。在这种情况下,人2在新系统上测试了2次,在旧系统上测试了1次,所以我想去掉他的所有数据(最后三行)。
你可以计算每个独特的值的频率,为每个 person
与 table
并选择所有唯一值的计数相同的组。
这可以在基础R中完成 。
subset(df, ave(Test, Person, FUN = function(x) length(unique(table(x)))) == 1)
# Person Test
#1 1 new
#2 1 new
#3 1 old
#4 1 old
dplyr
library(dplyr)
df %>% group_by(Person) %>% filter(n_distinct(table(Test)) == 1)
和 data.table
:
library(data.table)
setDT(df)[,.SD[uniqueN(table(Test)) == 1], Person]
下面是一个基本的R解决方案,使用 ave
和 table
.
i <- with(df1, ave(Test, Person, FUN = function(x){
all(table(x) == length(x) %/% 2)
}))
df1[as.logical(i), ]
# Person Test
#1 1 new
#2 1 new
#3 1 old
#4 1 old