[R] More elegant way of excluding rows with equal values in any 2 columns?
Dimitris Rizopoulos
d.rizopoulos at erasmusmc.nl
Mon Sep 21 20:44:13 CEST 2009
one way is the following:
index <- expand.grid(1:4, 1:4, 1:4, 1:4)
mat <- data.matrix(index)
keep <- apply(mat, 1, function (x, d)
length(unique(x)) == d, d = ncol(mat))
index[keep, ]
I hope it helps.
Best,
Dimitris
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!
>
>
>
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
More information about the R-help
mailing list