[R] More elegant way of excluding rows with equal values in any 2 columns?
Dimitri Liakhovitski
ld7631 at gmail.com
Mon Sep 21 21:01:50 CEST 2009
Thank you very much, both Dimitris and Erik.
Erik - you are right, I was trying to remove any duplication (i.e., if
there are the same values in 2 or 3 or 4 columns).
And it looks like that's what your solution does.
But doesn't it do the same thing as Dimitris' solution?
Dimitri
On Mon, Sep 21, 2009 at 2:55 PM, Erik Iverson <eiverson at nmdp.org> wrote:
> Hello,
>
> Do you mean exactly any 2 columns. What if the value is equal in more than 2 columns?
>
>>
>> I built a data frame "grid" (below) with 4 columns. I want to exclude
>> all rows that have equal values in ANY 2 columns. Here is how I am
>> doing it:
>>
>> index<-expand.grid(1:4,1:4,1:4,1:4)
>
> If a value is equal in 2 or more rows, i.e., duplicated, then the following should work, assuming index can be changed to a matrix for apply ...
>
> t3 <- index[apply(index, 1, function(x) all(!duplicated(x))),]
>
--
Dimitri Liakhovitski
Ninah.com
Dimitri.Liakhovitski at ninah.com
More information about the R-help
mailing list