Compare group of two columns and return index matches R

后端 未结 1 662
梦毁少年i
梦毁少年i 2020-12-21 14:37

Many thanks for reading. Apologies for what I\'m sure is a simple task.

I have a dataframe: (Edited: Added extra column not to be included in comparison)

<         


        
相关标签:
1条回答
  • 2020-12-21 15:29

    You could do something like this. It splits the row indices 1:nrow(df) according to unique sorted strings formed from the columns of df. The sorting ensures that A,B and B,A are treated identically.

    duplist <- split(1:nrow(df),apply(df,1,function(r) paste(sort(r),collapse=" ")))
    
    duplist
    $`alan edd`
    [1] 2 6
    
    $`alex david`
    [1] 1
    
    $`ben pete`
    [1] 3 4
    
    $`raymond richard`
    [1] 5
    
    0 讨论(0)
提交回复
热议问题