[R] Finding (swapped) repetitions of numbers pairs across two columns

Emmanuel Levy emmanuel.levy at gmail.com
Thu Dec 27 21:30:31 CET 2012


Hi,

I've had this problem for a while and tackled it is a quite dirty way
so I'm wondering is a better solution exists:

If we have two vectors:

v1 = c(0,1,2,3,4)
v2 = c(5,3,2,1,0)

How to remove one instance of the "3,1" / "1,3" double?

At the moment I'm using the following solution, which is quite horrible:

v1 = c(0,1,2,3,4)
v2 = c(5,3,2,1,0)
ft <- cbind(v1, v2)
direction = apply( ft, 1, function(x) return(x[1]>x[2]))
ft.tmp = ft
ft[which(direction),1] = ft.tmp[which(direction),2]
ft[which(direction),2] = ft.tmp[which(direction),1]
uniques     = apply( ft, 1, function(x) paste(x, collapse="%") )
uniques     = unique(uniques)
ft.unique   = matrix(unlist(strsplit(uniques,"%")), ncol=2, byrow=TRUE)


Any better solution would be very welcome!

All the best,

Emmanuel



More information about the R-help mailing list