вот решение data.table
образец данных
library( data.table )
DT <- fread("object person
1 1
1 2
1 3
2 2
2 3
3 3
4 1
4 4")
код
DT[, { #suppress immediate output by using {}
if( length(person) > 1) { #combinations only possible for >1 persons
temp <- combn( person, 2) #get all possible 2-person combinations
list( person1 = temp[1,], #add combinations to named list
person2 = temp[2,] )
} #end if
}, #now present the finalt output (i.e. the named list) ,
by = object ] #for each object
вывод
# object person1 person2
# 1: 1 1 2
# 2: 1 1 3
# 3: 1 2 3
# 4: 2 2 3
# 5: 4 1 4