我试图将下面矩阵的行转换为索引,转换为另一个矩阵的子集。第一个矩阵将生成四个索引,用于从名为data
的矩阵进行子集化,如下所示。第二个矩阵将生成六个索引,每个索引的长度为2,依此类推。
library(gtools) # library for combinations()
matrix(match(combinations(n=4,r=1,v=LETTERS[1:4]),LETTERS),ncol=1)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
matrix(match(combinations(n=4,r=2,v=LETTERS[1:4]),LETTERS),ncol=2)
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 2 3
[5,] 2 4
[6,] 3 4
matrix(match(combinations(n=4,r=3,v=LETTERS[1:4]),LETTERS),ncol=3)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 4
[3,] 1 3 4
[4,] 2 3 4
matrix(match(combinations(n=4,r=4,v=LETTERS[1:4]),LETTERS),ncol=4)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
data <- matrix(rnorm(16,0),ncol=4)
data[,1]
data[,2]
data[,3]
data[,4]
第二个矩阵将生成要使用的索引,如:
data[,c(1,2)]
data[,c(1,3)]
data[,c(1,4)]
data[,c(2,3)]
等
通用的矢量化函数在完成上述变更n
时会是什么样的?