[R] Finding (swapped) repetitions of numbers pairs across two columns
Marc Schwartz
marc_schwartz at me.com
Thu Dec 27 21:39:10 CET 2012
On Dec 27, 2012, at 2:30 PM, Emmanuel Levy <emmanuel.levy at gmail.com> wrote:
> 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
Try this:
> unique(t(apply(cbind(v1, v2), 1, sort)))
[,1] [,2]
[1,] 0 5
[2,] 1 3
[3,] 2 2
[4,] 0 4
Basically, sort each row so that you don't have to worry about the permutations of values, then get the unique rows as a result.
Regards,
Marc Schwartz
More information about the R-help
mailing list