R Remove rows if value in one column appears again in another column without loop [duplicate]
R Remove rows if value in one column appears again in another column without loop [duplicate]
This question already has an answer here:
I would like to remove cases where the combination of two columns also appears as a reverse combination in those two same columns
Below is the input and desired output.

It doesn't matter if the first or second occurrence is kept.
Data:
df <- data.frame(
"x1" = 1:6,
"x2" = c(2,1,4,3,6,5),
"x3" = c("a","b", "c","d","e","f"))
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
dput(your_input)
dput(head(you_input))
nopes = unname(as.list(DT[, 2:1])); res = DT[!nopes, on=names(DT)[1:2]]? Not sure if it works since there's no example to test with.– Frank
Sep 4 '18 at 19:51
nopes = unname(as.list(DT[, 2:1])); res = DT[!nopes, on=names(DT)[1:2]]
Sure, Try this: df <- data.frame("x1" = 1:6, "x2" = c(2,1,4,3,6,5), "x3" = c("a","b", "c","d","e","f"))
– a.m.
Sep 4 '18 at 20:30
with that data, you can do
subset(df,!duplicated(t(apply(df[-3],1,sort))))– Onyambu
Sep 4 '18 at 21:07
subset(df,!duplicated(t(apply(df[-3],1,sort))))
df[!duplicated(t(apply(df[-3],1,sort))),]– Onyambu
Sep 4 '18 at 21:08
df[!duplicated(t(apply(df[-3],1,sort))),]
1 Answer
1
The first suggestion worked just fine for me:
subset(df,!duplicated(t(apply(df[-3],1,sort))))
Can you please share your example input in a format I can copy/paste in to R? Images are a very unfriendly way to share data.
dput(your_input)ordput(head(you_input))is an easy way to make a copy/pasteable R object.– Gregor
Sep 4 '18 at 19:51