R语言 如何从一个变量列表中获取所有组合对,包括重复和自组合(用于相关性)?

8ftvxx2r  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(91)

我可以从下面的链接看到如何获得一个独特的组合列表,然后如何将其与自我组合相结合
Non-redundant version of expand.grid
例如。

factors <- c("a", "b", "c")

 all.combos <- t(combn(factors,2))

 dup.combos <- cbind(factors,factors)

 all.combosnew <- rbind(all.combos,dup.combos)

     factors factors
 [1,] "a"     "b"    
 [2,] "a"     "c"    
 [3,] "b"     "c"    
 [4,] "a"     "a"    
 [5,] "b"     "b"    
 [6,] "c"     "c"

字符串
然而,我还需要有重复的相关性,所以应该有9个组合

"a"     "a"
   "a"     "b"    
   "a"     "c"
   "b"     "a"
   "b"     "b"    
   "b"     "c"    
   "c"     "a"    
   "c"     "b"    
   "c"     "c"


这似乎应该是显而易见的,但我还没有找到它。任何建议都将是有益的。
最后,我需要使用cor.test将这个组合列表放入foreach循环中,并需要列出所有组合,以便将其转换为矩阵,例如3x3。

a       b      c
       1       x      x
       x       1      x
       x       x      1

juud5qan

juud5qan1#

正如Martin所建议的,在这种情况下,expand.grid是您的朋友。由于你没有提供数据,我创建了一些。以下可能不是最优雅的解决方案,但它应该给予你一个想法,你可以扩展。

# Using double letters so as to not overwrite "c"
factors <- c("aa", "bb", "cc")
# Create permutation list. Set stringsAsFactors to FALSE so we can use "get"
# later.
f2 <- expand.grid(factors, factors, stringsAsFactors = FALSE)
# Order on column 1
f2 <- f2[order(f2[, 1L]), ]
# Augment with correlation data.
set.seed(61L)
n <- 10000L
rho <- 0.7
aa <- rnorm(n, 0, 1)
bb <- rnorm(n, 0, 1)
cc <- rho * aa + sqrt(1 - rho ^ 2) * bb
# Always preallocate your return object if you can.
f2$cor <- 0
# Calculate correlations
for (i in seq_len(nrow(f2))) {
  f2$Cor[i] <- cor(get(f2$Var1[i]), get(f2$Var2[i]))
}

# Create Matrix
corM <- matrix(f2$Cor, ncol = 3L, dimnames = list(factors, factors))

corM
aa  1.00000000 -0.01064047 0.6978159
bb -0.01064047  1.00000000 0.7088116
cc  0.69781586  0.70881156 1.0000000

字符串

相关问题