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

后端 未结 7 733
借酒劲吻你
借酒劲吻你 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:39

    Here is @Joshua Ulrich's solution as a function. This format allows you to use this code in the same fashion that you would use duplicated():

    allDuplicated <- function(vec){
      front <- duplicated(vec)
      back <- duplicated(vec, fromLast = TRUE)
      all_dup <- front + back > 0
      return(all_dup)
    }
    

    Using the same example:

    vec <- c("a", "b", "c","c","c") 
    allDuplicated(vec) 
    [1] FALSE FALSE  TRUE  TRUE  TRUE
    
    

提交回复
热议问题