Remove duplicated rows

前端 未结 11 2086
清酒与你
清酒与你 2020-11-22 00:00

I have read a CSV file into an R data.frame. Some of the rows have the same element in one of the columns. I would like to remove rows that are duplicates in th

11条回答
  •  生来不讨喜
    2020-11-22 00:52

    For people who have come here to look for a general answer for duplicate row removal, use !duplicated():

    a <- c(rep("A", 3), rep("B", 3), rep("C",2))
    b <- c(1,1,2,4,1,1,2,2)
    df <-data.frame(a,b)
    
    duplicated(df)
    [1] FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE
    
    > df[duplicated(df), ]
      a b
    2 A 1
    6 B 1
    8 C 2
    
    > df[!duplicated(df), ]
      a b
    1 A 1
    3 A 2
    4 B 4
    5 B 1
    7 C 2
    

    Answer from: Removing duplicated rows from R data frame

提交回复
热议问题