Finding ALL duplicate rows, including “elements with smaller subscripts”

后端 未结 7 724
借酒劲吻你
借酒劲吻你 2020-11-21 07:55

R\'s duplicated returns a vector showing whether each element of a vector or data frame is a duplicate of an element with a smaller subscript. So if rows 3, 4,

相关标签:
7条回答
  • 2020-11-21 08:41

    duplicated has a fromLast argument. The "Example" section of ?duplicated shows you how to use it. Just call duplicated twice, once with fromLast=FALSE and once with fromLast=TRUE and take the rows where either are TRUE.


    Some late Edit: You didn't provide a reproducible example, so here's an illustration kindly contributed by @jbaums

    vec <- c("a", "b", "c","c","c") 
    vec[duplicated(vec) | duplicated(vec, fromLast=TRUE)]
    ## [1] "c" "c" "c"
    

    Edit: And an example for the case of a data frame:

    df <- data.frame(rbind(c("a","a"),c("b","b"),c("c","c"),c("c","c")))
    df[duplicated(df) | duplicated(df, fromLast=TRUE), ]
    ##   X1 X2
    ## 3  c  c
    ## 4  c  c
    
    0 讨论(0)
提交回复
热议问题