[R] Extracing Unique Rows based on 2 Column

Johannes Huesing johannes at huesing.name
Mon Nov 30 08:14:47 CET 2015


Ragia Ibrahim <ragia11 at hotmail.com> [Mon, Nov 30, 2015 at 04:55:08AM CET]:
>Dear group,
>kindly,  I have a data frame, as follows:
>
>
> Measure_id i j value      rank
>
>I want to select distinct rows based on two coulmn ( Measure_id  and i )
>

I didn't get your example code to run but the following works for me:

dfr <- data.frame(Measure_id = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5,
5, 5, 5), i = c(2, 5, 2, 5, 7, 2, 7, 5, 2, 2, 7, 2, 5, 7, 2,
2, 2, 5, 5, 7, 7, 2, 5, 2, 2, 5, 7, 7, 2, 2, 5, 2, 5, 7, 7),
   j = c(3, 1, 1, 2, 3, 4, 5, 2, 1, 4, 5, 3, 1, 3, 1, 3, 4,
   1, 2, 3, 5, 4, 2, 1, 3, 1, 3, 5, 1, 4, 2, 3, 1, 3, 5), value = c(2,
   2, 1.5, 1.5, 1.5, 1, 1, 2.5, 2, 2, 2, 1.5, 1.5, 1, 1, 0,
   0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 2, 2, 2, 1, 1, 1, 1))

dfr[!duplicated(dfr[, c("Measure_id", "i")]), ]

This returns

    Measure_id i j value
1           1 2 3   2.0
2           1 5 1   2.0
5           1 7 3   1.5
8           2 5 2   2.5
9           2 2 1   2.0
11          2 7 5   2.0
15          3 2 1   1.0
18          3 5 1   0.0
20          3 7 3   0.0
22          4 2 4   1.0
23          4 5 2   1.0
27          4 7 3   0.0
29          5 2 1   2.0
31          5 5 2   2.0
34          5 7 3   1.0

Is that what you were aiming at?

Note that I had to find the solution myself, which I did, 
thanks to the documentation I got by ?unique, which pointed
me to duplicated().

-- 
Johannes Hüsing               
http://derwisch.wikidot.com
Threema-ID: VHVJYH3H



More information about the R-help mailing list