Как насчет применения функции, аналогичной в вашем примере, к матрице комбинаций переменных:
library(dplyr)
## define the data frame
df = as.data.frame(cbind(c(.59, .72, 1.31, 4.32),
c(.78, .91, 1.21, 1.52),
c(.91, .73, .9, 3.2),
c(1,2,3,2)), stringsAsFactors = FALSE)
names(df) = c("V1", "V2", "V3", "group")
## generate a matrix with the unique combinations of groups
combinations = combn(x = unique(df$group), m = 2)
## apply a function over the matrix of group combinations to determine
## the distance between the variable observations
distlist = lapply(seq(from = 1, to = ncol(combinations)), function(i){
tmpdist = df %>% filter(group %in% combinations[,i]) %>%
select(-group) %>%
dist()
return(cbind(combinations[1,i], combinations[2,i], tmpdist))
})
## combine the list into a dataframe
dists = do.call(rbind, distlist)
names(dists) = c("group1", "group2", "dist")