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.



SS of Tables



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.





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) or dput(head(you_input)) is an easy way to make a copy/pasteable R object.
– Gregor
Sep 4 '18 at 19:51


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))))

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

PHP code is not being executed, instead code shows on the page

Malaysia to Papua New Guinea by motorcycle?