2016-07-06 78 views
4

我對按組的組合提出了一個問題。按組中的組合R

我的小樣本如下:

sample <- data.frame(
    group=c("a","a","a","a","b","b","b"), 
    number=c(1,2,3,2,4,5,3) 
) 

如果我申請的combn的功能將數據幀時,它給了我下面的結果,這是價值觀下的「數字」的所有組合柱無論哪個組的值屬於:

  [,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 

我用於結果上面的代碼如下:

t(combn((sample$number), 2)) 

但是,我希望得到組內的組合結果(即「a」,「b」)。因此,我想要得到的結果應該是這樣的:

 [,1] [,2] [,3] 
[1,] a 1 2 
[2,] a 1 3 
[3,] a 1 2 
[4,] a 2 3 
[5,] a 2 2 
[6,] a 3 2 
[7,] b 4 5 
[8,] b 4 3 
[9,] b 5 3 

除了組合,我想獲得指示 組列。

回答

3

我們可以通過函數data.table

library(data.table) 
setDT(sample)[, {i1 <- combn(number, 2) 
        list(i1[1,], i1[2,]) }, by = group] 
# group V1 V2 
#1:  a 1 2 
#2:  a 1 3 
#3:  a 1 2 
#4:  a 2 3 
#5:  a 2 2 
#6:  a 3 2 
#7:  b 4 5 
#8:  b 4 3 
#9:  b 5 3 

或壓縮選項使用一組將

setDT(sample)[, transpose(combn(number, 2, FUN = list)), by = group] 

或者使用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))) 
3

這裏有一個BA使用(1)split創建每個唯一組條目的數據幀列表,(2)lapply以循環遍歷每個列表元素並使用combn,(3)do.call(rbind, ...)來計算組合,以將列表元素收回到一個data.frame

do.call(rbind, lapply(split(sample, sample$group), { 
    function(x) data.frame(group = x$group[1], t(combn(x$number, 2))) 
})) 

# group X1 X2 
#a.1  a 1 2 
#a.2  a 1 3 
#a.3  a 1 2 
#a.4  a 2 3 
#a.5  a 2 2 
#a.6  a 3 2 
#b.1  b 4 5 
#b.2  b 4 3 
#b.3  b 5 3 

並有dplyr選項:

library(dplyr) 
sample %>% group_by(group) %>% do(data.frame(t(combn(.$number, 2)))) 
#Source: local data frame [9 x 3] 
#Groups: group [2] 
# 
# group X1 X2 
# (fctr) (dbl) (dbl) 
#1  a  1  2 
#2  a  1  3 
#3  a  1  2 
#4  a  2  3 
#5  a  2  2 
#6  a  3  2 
#7  b  4  5 
#8  b  4  3 
#9  b  5  3