Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate duplicates in R [duplicate]

Tags:

r

If I have a df like this

data<-data.frame(id=c(1,1,3,4),n=c("x","y","e","w"))
data
  id n
1  1 x
2  1 y
3  3 e
4  4 w

I want to get a new df like this:

data
  id n
3  3 e
4  4 w

That is, I want it to remove all repeating rows. I've tried functions like distinct from dplyr but it always gets one of the repeating rows.

like image 758
José Carlos Rojas Avatar asked Nov 27 '25 12:11

José Carlos Rojas


1 Answers

Another subset option with ave

subset(
    data,
    ave(n, id, FUN = length) == 1
)

gives

  id n
3  3 e
4  4 w
like image 60
ThomasIsCoding Avatar answered Dec 01 '25 00:12

ThomasIsCoding