r - Unique combinations of two vectors without pair repetition -
this question has answer here:
- select equivalent rows [a-b & b-a] 1 answer
i have problem need keep unique rows not have repeated pair e.g. if have table x1=c(1,2,3) , x2=c(2,1,3) first , third row kept. row (2,1) thrown out because (1,2) exists. can help?
thanks!
we can sort
row using apply
, logical index using duplicated
remove duplicate rows.
df1[!duplicated(t(apply(df1, 1, sort))),] # x1 x2 #1 1 2 #2 1 3 #3 1 1 #4 2 4 #6 2 2 #7 3 4 #8 3 2
data
df1 <- structure(list(x1 = c(1l, 1l, 1l, 2l, 2l, 2l, 3l, 3l, 3l, 4l, 4l), x2 = c(2l, 3l, 1l, 4l, 1l, 2l, 4l, 2l, 1l, 2l, 2l)), .names = c("x1", "x2"), class = "data.frame", row.names = c(na, -11l))
Comments
Post a Comment