Removing duplicate values row-wise in R

后端 未结 1 1093
谎友^
谎友^ 2020-12-17 03:21

I am working with a dataset in R, and I have a problem that I can\'t seem to figure out. My data currently looks like this:

Team    Person1   Person2   Pers         


        
相关标签:
1条回答
  • 2020-12-17 04:08

    Yuo can identify duplicated person ids across the rows using apply

    dat$dups <- apply(dat[-1], 1, function(i) any(duplicated(i[!is.na(i)])))
    

    or as Simon O'Hanlon pointed out in the comments

    dat$dups <- apply(dat[-1], 1, function(i) any(duplicated(i, incomparables = NA)))
    

    You could then use this to either find the team numbers that have duplicates or to exclude them:

    # Return teams that have duplicate person ids
    dat$Team[ dat$dups ]
    # Exclude rows with duplicates
    dat[ ! dat$dups , ]
    
    0 讨论(0)
提交回复
热议问题