Combinations by group in R -
i have question combinations group.
my mini-sample looks this:
sample <- data.frame( group=c("a","a","a","a","b","b","b"), number=c(1,2,3,2,4,5,3) )
if apply function of combn
to data frame,it gives me following result, combinations of values under 'number' column regardless of group value belongs to:
[,1] [,2] [1,] 1 2 [2,] 1 3 [3,] 1 2 [4,] 1 4 [5,] 1 5 [6,] 1 3 [7,] 2 3 [8,] 2 2 [9,] 2 4 [10,] 2 5 [11,] 2 3 [12,] 3 2 [13,] 3 4 [14,] 3 5 [15,] 3 3 [16,] 2 4 [17,] 2 5 [18,] 2 3 [19,] 4 5 [20,] 4 3 [21,] 5 3
the code used results above follows:
t(combn((sample$number), 2))
however, combination results within group (i.e., "a", "b"). therefore, result want should this:
[,1] [,2] [,3] [1,] 1 2 [2,] 1 3 [3,] 1 2 [4,] 2 3 [5,] 2 2 [6,] 3 2 [7,] b 4 5 [8,] b 4 3 [9,] b 5 3
in addition combinations, column indicating group.
thank in advance. looking forward replies!
we can use group function data.table
library(data.table) setdt(sample)[, {i1 <- combn(number, 2) list(i1[1,], i1[2,]) }, = group] # group v1 v2 #1: 1 2 #2: 1 3 #3: 1 2 #4: 2 3 #5: 2 2 #6: 3 2 #7: b 4 5 #8: b 4 3 #9: b 5 3
or compact option be
setdt(sample)[, transpose(combn(number, 2, fun = list)), = group]
or using base r
lst <- by(sample$number, sample$group, fun = combn, m= 2) data.frame(group = rep(unique(as.character(sample$group)), sapply(lst, ncol)), t(do.call(cbind, lst)))
Comments
Post a Comment