Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - function like duplicated that removes all of the duplicated instances

Tags:

r

Let's say we have the following:

c("A", "A", "B") %>% 
   cbind(1:3) %>% 
   data.frame() -> testdf

We want to remove from the dataframe all instances where there was a duplicate in the first variable. Usually we would use something like this:

testdf2 <- testdf[!duplicated(testdf$.),]

However, testdf2 looks like this:

. V2
A  1
B  3

This is not what I was looking for - since the value A was duplicated, I want to remove all cases that have A in the first variable. I want my output to be like this:

. V2
B  3

Is there a function that could produce this?

like image 816
J. Doe Avatar asked Dec 13 '22 08:12

J. Doe


1 Answers

try testdf[!duplicated(testdf$.)&!duplicated(testdf$.,fromLast = TRUE),]

like image 159
Frank Zhang Avatar answered Mar 22 '23 22:03

Frank Zhang