如何在R中使用条件来删除记录

问题描述 投票:-1回答:1

我有一个像这样的数据框,我想把新系统和旧系统上的测试行数不相等的行去掉。

Person Test  
1 new  
1 new  
1 old  
1 old  
2 new  
2 new  
2 old

我想删除新系统和旧系统中测试次数不相等的行。在这种情况下,人2在新系统上测试了2次,在旧系统上测试了1次,所以我想去掉他的所有数据(最后三行)。

r data-cleaning
1个回答
1
投票

你可以计算每个独特的值的频率,为每个 persontable 并选择所有唯一值的计数相同的组。

这可以在基础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]

1
投票

下面是一个基本的R解决方案,使用 avetable.

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
© www.soinside.com 2019 - 2024. All rights reserved.