R中列表形式向量元素的成对组合
我的向量是R中列表形式向量元素的成对组合,r,combinations,R,Combinations,我的向量是all\u comparison那么combn(all\u comparison,2)呢 要将所有内容都列在一个方便的列表中,您可以这样做 xy <- as.data.frame(combn(all_comparisons, 2)) sapply(xy, function(x) as.character(x), simplify = FALSE) $V1 [1] "T1" "T2" $V2 [1] "T1" "T3" $V3 [1] "T1" "T4" $V4 [1]
all\u comparison那么combn(all\u comparison,2)
呢
要将所有内容都列在一个方便的列表中,您可以这样做
xy <- as.data.frame(combn(all_comparisons, 2))
sapply(xy, function(x) as.character(x), simplify = FALSE)
$V1
[1] "T1" "T2"
$V2
[1] "T1" "T3"
$V3
[1] "T1" "T4"
$V4
[1] "T2" "T3"
$V5
[1] "T2" "T4"
$V6
[1] "T3" "T4"
这就是你想要的吗
split(combn(all_comparisons,2), col(combn(all_comparisons,2)))
$`1`
[1] "T1" "T2"
$`2`
[1] "T1" "T3"
$`3`
[1] "T1" "T4"
$`4`
[1] "T2" "T3"
$`5`
[1] "T2" "T4"
$`6`
[1] "T3" "T4"
@JorisChau这给了我矩阵,我想要列表格式的很好,但是如果所有的比较都很大,那么combn
可能会非常昂贵。为什么不干脆combn(所有的比较,2,simplify=F)
xy <- combn(all_comparisons, 2)
split(t(xy), f = 1:ncol(xy))
split(combn(all_comparisons,2), col(combn(all_comparisons,2)))
$`1`
[1] "T1" "T2"
$`2`
[1] "T1" "T3"
$`3`
[1] "T1" "T4"
$`4`
[1] "T2" "T3"
$`5`
[1] "T2" "T4"
$`6`
[1] "T3" "T4"