[R] More elegant way of excluding rows with equal values in any 2 columns?

Dimitri Liakhovitski ld7631 at gmail.com
Mon Sep 21 21:19:32 CEST 2009


Thanks a lot everyone - it's been extremely helpful!
Dimitri

On Mon, Sep 21, 2009 at 3:16 PM, Jorge Ivan Velez
<jorgeivanvelez at gmail.com> wrote:
>
> Hi Dimitri,
> Try also either
> index[apply(index, 1, function( x ) length( unique( x ) ) == 4 ),]
> or
> # install.packages('e1071')
> require(e1071)
> permutations(4)
> HTH,
> Jorge
>
> On Mon, Sep 21, 2009 at 2:14 PM, Dimitri Liakhovitski <> wrote:
>>
>> Hello, dear R-ers!
>>
>> 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)
>> dim(index)
>> # Deleting rows that have identical values in any two columns (1 line of
>> code):
>>
>> index<-index[!(index$Var1==index$Var2)&!(index$Var1==index$Var3)&!(index$Var1==index$Var4)&!(index$Var2==index$Var3)&!(index$Var2==index$Var4)&!(index$Var3==index$Var4),]
>> dim(index)
>> index
>>
>>
>> I was wondering if there is a more elegant way of doing it - because
>> as the number of columns increases, the amount of code one would have
>> to write increases A LOT.
>>
>> Thank you very much for any suggestion!
>>
>>
>>
>> --
>> Dimitri Liakhovitski
>> Ninah.com
>> Dimitri.Liakhovitski at ninah.com
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Dimitri Liakhovitski
Ninah.com
Dimitri.Liakhovitski at ninah.com




More information about the R-help mailing list