Вот решение в R:
Воссоздать данные
x <- data.frame(
id = letters[1:5],
attr1 = c(2,3,2,1,2),
attr2 = c(5,1,NA,2,1),
attr3 = c(7,NA,NA,5,3))
x
id attr1 attr2 attr3
1 a 2 5 7
2 b 3 1 NA
3 c 2 NA NA
4 d 1 2 5
5 e 2 1 3
Создать функцию для перечисления всех комбинаций
make_combinations <- function(data, size){
t1 <- apply(data[, -1], 1, function(data)unname(sort(data)))
t2 <- lapply(t1, function(xt){if(length(xt)>=size){combn(xt, size)}})
t3 <- sapply(t2[!is.na(t2)],
function(chunk){if(!is.null(chunk))apply(chunk, 2, function(x)paste(x, collapse=","))})
t4 <- unlist(t3)
t4
}
Создать вторую функцию для подсчета комбинаций
count_combinations <- function(data, nn=2:3){
tmp <- unlist(lapply(nn, function(n)make_combinations(data, n)))
sort(table(tmp), decreasing=TRUE)
}
Результаты:
count_combinations(x, 2:3)
1,2 1,3 2,5 1,2,3 1,2,5 1,5 2,3 2,5,7 2,7 5,7
2 2 2 1 1 1 1 1 1 1