-1

I have this 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

I want to search for rows that have the same value in column1 and none of them have +1 in column 2. So, in this case, for example, rows that have value x=2 have no y=1, so I want to remove them. same thing also happens for rows with x=9 and x=1.

In another word, if we create subsets of the data by which in each subset, all the x values are the same, then any subset that doesn't have y=1 should be discarded.

Do you have any suggestion? If it is not clear, I will try to elaborate better!

4

4 に答える 4

0

これは、d$x==2 のいずれかの行で d$y の値が 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
于 2013-10-16T15:57:33.637 に答える
0

最初に y==1 を見つけてから、重複する x をチェックする方が簡単ではないでしょうか?

as.data.table(d)[y==1][x %in% x[duplicated(x)]]
于 2013-10-16T16:51:43.740 に答える