R:提取数据帧中另一列(y)中至少有一个+1的一列(x)的值
我有这个data.frame:R:提取数据帧中另一列(y)中至少有一个+1的一列(x)的值,r,remove-if,R,Remove If,我有这个data.frame: > d x y 1 6 1 2 2 -1 3 3 -1 4 2 -1 5 3 1 6 1 -1 7 4 1 8 7 -1 9 3 -1 10 4 -1 11 8 1 12 4 -1 13 2 -1 14 9 -1 15 5 1 16 7 1 17 6 -1 18 7 -1 19 3 -1 20 2 -1 我想搜索在第1列中具有相同值但在第2列中没有一行具有+1的行。例如,在本例中,值为x=2的行没有y=1,所以我想删除
> d
x y
1 6 1
2 2 -1
3 3 -1
4 2 -1
5 3 1
6 1 -1
7 4 1
8 7 -1
9 3 -1
10 4 -1
11 8 1
12 4 -1
13 2 -1
14 9 -1
15 5 1
16 7 1
17 6 -1
18 7 -1
19 3 -1
20 2 -1
我想搜索在第1列中具有相同值但在第2列中没有一行具有+1的行。例如,在本例中,值为x=2的行没有y=1,所以我想删除它们。对于x=9和x=1的行也会发生同样的情况
换句话说,如果我们创建数据的子集,在每个子集中,所有的x值都是相同的,那么任何没有y=1的子集都应该被丢弃
你有什么建议吗?如果不清楚,我会尽量详细说明 我想这就是你想要的:
d[d$x %in% subset(aggregate(y ~ x, d, max), y == 1)$x, ]
对于每个唯一的x,获取最大值:
aggregate(y ~ x, d, max)
只需返回y的最大值为1的x
现在拉出x在这组x值中的行
以下是一个简单的解决方案:
>df[with(df,x %in% unique(x[y==1])),]
x y
1 6 1
3 3 -1
5 3 1
7 4 1
8 7 -1
9 3 -1
10 4 -1
11 8 1
12 4 -1
15 5 1
16 7 1
17 6 -1
18 7 -1
19 3 -1
或者,等价地:df[df$x%in%uniquedf$x[df$y==1],]这将是对d$y在其中d$x==2的任何行中包含值1的可能性的测试:
any( d[d$x==2, "y"] == 1 )
如果该可能性成立,则返回一个数据帧,该数据帧使用一些布尔代数和逻辑索引删除了所有d$x==2行:
d[ !as.logical( d$x == 2 * any( d[d$x==2, "y"] == 1 ) ) , ]
注意:值2不符合您设置的狭窄条件
如果要将该规则应用于d$x的所有唯一值,并应用更一般的排除条件no y>0
lmat <- t( sapply( unique(d$x) , function(val)
as.logical( d[["x"]] == val * any( d[d[["x"]]==val, "y"] > 1 ) ) ) )
# Now use `any` to determine which rows to exclude.
# One does need to transpose that matrix of excluded logicals.
d[ ! apply( t(lmat) , 1, any), ]
x y
2 2 -1
4 2 -1
6 1 -1
13 2 -1
14 9 -1
20 2 -1
首先找到y==1,然后检查重复的x不是更简单吗
as.data.table(d)[y==1][x %in% x[duplicated(x)]]
你能告诉我们你试过什么吗。给我们我们可以使用的数据,我们可以向您展示,您只需按x值分组,这是data.table或plyr的工作
lmat <- t( sapply( unique(d$x) , function(val)
as.logical( d[["x"]] == val * any( d[d[["x"]]==val, "y"] > 1 ) ) ) )
# Now use `any` to determine which rows to exclude.
# One does need to transpose that matrix of excluded logicals.
d[ ! apply( t(lmat) , 1, any), ]
x y
2 2 -1
4 2 -1
6 1 -1
13 2 -1
14 9 -1
20 2 -1
as.data.table(d)[y==1][x %in% x[duplicated(x)]]