R中没有重复的组合

时间:2018-09-06 10:45:28

标签: r combinations

我有:

A1 <-   c(1,    1,  0,  93, 19, 8.7)
A2 <-   c(2,    1,  0,  226,    45, 6.9)
A3 <-   c(3,    2,  1,  86, 17, 8.5)                    
B1 <-   c(1,    4,  1,  113,    23, 7  )
B2 <-   c(2,    4,  0,  108,    22, 7  )
B3 <-   c(3,    5,  0,  246,    49, 9.5)                        
C1 <-   c(1,    9,  0,  127,    25, 8  )
C2 <-   c(2,    10, 1,  116,    23, 8  )
C3 <-   c(3,    10, 0,   159.3, 32,     8  )                        
D1 <-   c(1,    11, 0,  431,    86, 10 )
D2 <-   c(2,    12, 0,  52, 10, 8  )
D3 <-   c(3,    12, 1,  171,    34, 4.2)                        
E1 <-   c(1,    16, 0,  270,    54, 7  )
E2 <-   c(2,    16, 0,  86, 17, 8  )
E3 <-   c(3,    16, 1,  237,    47, 9  )                        
F1 <-   c(1,    19, 1,  98, 20, 8  )
F2 <-   c(2,    20, 1,  164,    33, 8  )
F3 <-   c(3,    21, 0,  99, 25, 8  )                        
G1 <-   c(1,    22, 1,  125,    25, 9  )
G2 <-   c(2,    25, 0,  222,    44, 9  )
G3 <-   c(3,    25, 0,  161,    32, 11 )                        
H1 <-   c(1,    26, 1,  52, 10, 6.8)
H2 <-   c(2,    28, 0,  405,    81, 9  )
H3 <-   c(3,    29, 0,  71, 14, 8  )
DG = matrix( c(A1,A2,A3,B1,B2,B3,C1,C2,C3,D1,D2,D3,E1,E2,E3,F1,F2,F3,G1,G2,G3,H1,H2,H3), nrow=6,  ncol=24 , dimnames=list( c("number","time","status","Trig","vldl","low"), c("A1","A2","A3","B1","B2","B3","C1","C2","C3","D1","D2","D3","E1","E2","E3","F1","F2","F3","G1","G2","G3","H1","H2","H3")))
TD <- t(DG)

想要在行之间进行组合,而无需重复:

lapply(combn(24,3,simplify=FALSE),function(x) TD[x,])#某些结果将显示为:

[[1901]]
   number time status Trig vldl low
E2      2   16      0   86   17   8
G3      3   25      0  161   32  11
H3      3   29      0   71   14   8

[[1902]]
   number time status Trig vldl low
E2      2   16      0   86   17 8.0
H1      1   26      1   52   10 6.8
H2      2   28      0  405   81 9.0

[[1903]]
   number time status Trig vldl low
E2      2   16      0   86   17 8.0
H1      1   26      1   52   10 6.8
H3      3   29      0   71   14 8.0

[[1904]]
   number time status Trig vldl low
E2      2   16      0   86   17   8
H2      2   28      0  405   81   9
H3      3   29      0   71   14   8

[[1905]]
   number time status Trig vldl low
E3      3   16      1  237   47   9
F1      1   19      1   98   20   8
F2      2   20      1  164   33   8

[[1906]]
   number time status Trig vldl low
E3      3   16      1  237   47   9
F1      1   19      1   98   20   8
F3      3   21      0   99   25   8

[[1907]]
   number time status Trig vldl low
E3      3   16      1  237   47   9
F1      1   19      1   98   20   8
G1      1   22      1  125   25   9

[[1908]]
   number time status Trig vldl low
E3      3   16      1  237   47   9
F1      1   19      1   98   20   8
G2      2   25      0  222   44   9

[[1909]]
   number time status Trig vldl low
E3      3   16      1  237   47   9
F1      1   19      1   98   20   8
G3      3   25      0  161   32  11

[[1910]]
   number time status Trig vldl low
E3      3   16      1  237   47 9.0
F1      1   19      1   98   20 8.0
H1      1   26      1   52   10 6.8

[[1911]]
   number time status Trig vldl low
E3      3   16      1  237   47   9
F1      1   19      1   98   20   8
H2      2   28      0  405   81   9

[[1912]]
   number time status Trig vldl low
E3      3   16      1  237   47   9
F1      1   19      1   98   20   8
H3      3   29      0   71   14   8

[[1913]]
   number time status Trig vldl low
E3      3   16      1  237   47   9
F2      2   20      1  164   33   8
F3      3   21      0   99   25   8

   number time status Trig vldl low
G2      2   25      0  222   44 9.0
H1      1   26      1   52   10 6.8
H3      3   29      0   71   14 8.0

[[2020]]
   number time status Trig vldl low
G2      2   25      0  222   44   9
H2      2   28      0  405   81   9
H3      3   29      0   71   14   8

[[2021]]
   number time status Trig vldl  low
G3      3   25      0  161   32 11.0
H1      1   26      1   52   10  6.8
H2      2   28      0  405   81  9.0

[[2022]]
   number time status Trig vldl  low
G3      3   25      0  161   32 11.0
H1      1   26      1   52   10  6.8
H3      3   29      0   71   14  8.0

[[2023]]
   number time status Trig vldl low
G3      3   25      0  161   32  11
H2      2   28      0  405   81   9
H3      3   29      0   71   14   8

[[2024]]
   number time status Trig vldl low
H1      1   26      1   52   10 6.8
H2      2   28      0  405   81 9.0
H3      3   29      0   71   14 8.0

这在某些结果形式R之上,但我想在行之间进行组合而不重复字母或所有matirix都使用半字母作为:

true  resut :

    [[1901]]
       number time status Trig vldl low
    E2      2   16      0   86   17   8
    G3      3   25      0  161   32  11
    H3      3   29      0   71   14   8

    [[1908]]
       number time status Trig vldl low
    E3      3   16      1  237   47   9
    F1      1   19      1   98   20   8
    G2      2   25      0  222   44   9

    FALSE RESULT :
    [[2021]]
       number time status Trig vldl  low
    G3      3   25      0  161   32 11.0
    H1      1   26      1   52   10  6.8
    H2      2   28      0  405   81  9.0

    [[2023]]
       number time status Trig vldl low
    G3      3   25      0  161   32  11
    H2      2   28      0  405   81   9
    H3      3   29      0   71   14   8

    [[2024]]
       number time status Trig vldl low
    H1      1   26      1   52   10 6.8
    H2      2   28      0  405   81 9.0
    H3      3   29      0   71   14 8.0

请帮助我,可以将组合中的重复项删除为(H1,H2,H3) OR ( A1,A2,A3) OR (H1,A1,A2) ......等等,谢谢

1 个答案:

答案 0 :(得分:1)

您可以先生成块的所有组合(A,B,...),然后为每个块组合枚举行的所有组合:

rowidx <- as.matrix(do.call(expand.grid, rep(list(1:3), 3)))
gid <- cumsum(rep(3, 8)) - 3

res <- lapply(combn(1:8, 3, simplify = FALSE), function(x){
    lapply(1:nrow(rowidx), function(y){
        TD[gid[x] + rowidx[y, ], ]
    })
})
res <- unlist(res, recursive = FALSE)

# sample example output
sample(res, 3)

# [[1]]
#    number time status Trig vldl low
# A3      3    2      1   86   17 8.5
# E1      1   16      0  270   54 7.0
# G1      1   22      1  125   25 9.0
# 
# [[2]]
#    number time status Trig vldl low
# C1      1    9      0  127   25 8.0
# F2      2   20      1  164   33 8.0
# H1      1   26      1   52   10 6.8
# 
# [[3]]
#    number time status Trig vldl low
# B3      3    5      0  246   49 9.5
# C2      2   10      1  116   23 8.0
# E3      3   16      1  237   47 9.0
# 

通用版本:

gid <- cumsum(rep(3, 8)) - 3
n <- 4  # change n to whatever value (between 2..8) you want.
rowidx <- as.matrix(do.call(expand.grid, rep(list(1:3), n)))

res <- lapply(combn(1:8, n, simplify = FALSE), function(x){
    lapply(1:nrow(rowidx), function(y){
        TD[gid[x] + rowidx[y, ], ]
    })
})
res <- unlist(res, recursive = FALSE)