2013-05-01 69 views
2

我有的高达4列(即第1列,第2列,第3列和第4列)使用原矩阵得到了几个新的矩阵

V1 V2 V3 V4 
1 1 1 1 1 
2 1 1 1 1 
3 1 -1 -1 -1 
4 1 -1 -1 -1 
5 2 1 1 -1 
6 2 1 1 -1 
7 2 -1 -1 1 
8 2 -1 -1 1 
9 3 1 -1 1 
10 3 1 -1 1 
11 3 -1 1 -1 
12 3 -1 1 -1 
13 4 1 -1 -1 
14 4 1 -1 -1 
15 4 -1 1 1 
16 4 -1 1 1 

我的问题是由矩阵:我想用这4列来获得3个新的矩阵。
这些矩阵由以下列组成:12, 13, 14, 23, 24, 34, 123, 124, 134, 234, 1234. 这里我用12代表column 1 * column 2

第一矩阵具有6列:12, 13, 14, 23, 24, 34

X1 X2 X3 X4 X5 X6 
1 1 1 1 1 1 1 
2 1 1 1 1 1 1 
3 -1 -1 -1 1 1 1 
4 -1 -1 -1 1 1 1 
5 2 2 -2 1 -1 -1 
6 2 2 -2 1 -1 -1 
7 -2 -2 2 1 -1 -1 
8 -2 -2 2 1 -1 -1 
9 3 -3 3 -1 1 -1 
10 3 -3 3 -1 1 -1 
11 -3 3 -3 -1 1 -1 
12 -3 3 -3 -1 1 -1 
13 4 -4 -4 -1 -1 1 
14 4 -4 -4 -1 -1 1 
15 -4 4 4 -1 -1 1 
16 -4 4 4 -1 -1 1 

第二矩阵具有4列:123, 124, 134, 234
而最后矩阵具有一列:1234

没有人有一些简单的代码段去做这个?感谢大家的帮助。

回答

3

我们可以创建一个小功能来实现这个任务(假定他的岗位使用的OP类型相同符号的)

create_prodmat <- function(mat, cols) { 

index <- lapply(strsplit(cols, ""), as.numeric) 

res <- do.call("cbind", 
       lapply(index, function(i) apply(mat[, i, drop = FALSE], 1, prod)) 
       ) 
colnames(res) <- cols 
res 

} 



mat <- matrix(rep(c(1, 2, 3), each = 4), ncol = 3) 
mat 

##  [,1] [,2] [,3] 
## [1,] 1 2 3 
## [2,] 1 2 3 
## [3,] 1 2 3 
## [4,] 1 2 3 


cols1 <- c("12", "13", "23") 
cols2 <- "13" 

create_prodmat(mat, cols1) 

##  12 13 23 
## [1,] 2 3 6 
## [2,] 2 3 6 
## [3,] 2 3 6 
## [4,] 2 3 6 


create_prodmat(mat, cols2) 

##  13 
## [1,] 3 
## [2,] 3 
## [3,] 3 
## [4,] 3 
1

下面应该这样做。
我把它按行分解,所以应该很容易遵循。如果您需要解释,请告诉我。

newColumns <- "14, 23, 24, 34, 123, 124, 134, 234, 1234" 

splat1 <- strsplit(newColumns, ", ")[[1]] 

splat <- strsplit(splat1, "") 

splat <- lapply(splat, as.numeric) 

results <- sapply(splat, function(cols) apply(master.matrix[, cols, drop=FALSE], 1, prod)) 

first <- results[, 1:4] 
second <- results[, 5:8] 
third <- results[, 9, drop=FALSE] 

first; second; third 

     [,1] [,2] [,3] [,4] 
    1  1 1 1 1 
    2  1 1 1 1 
    3 -1 1 1 1 
    4 -1 1 1 1 
    5 -2 1 -1 -1 
    6 -2 1 -1 -1 
    7  2 1 -1 -1 
    8  2 1 -1 -1 
    9  3 -1 1 -1 
    10 3 -1 1 -1 
    11 -3 -1 1 -1 
    12 -3 -1 1 -1 
    13 -4 -1 -1 1 
    14 -4 -1 -1 1 
    15 4 -1 -1 1 
    16 4 -1 -1 1 
    [,1] [,2] [,3] [,4] 
    1  1 1 1 1 
    2  1 1 1 1 
    3  1 1 1 -1 
    4  1 1 1 -1 
    5  2 -2 -2 -1 
    6  2 -2 -2 -1 
    7  2 -2 -2 1 
    8  2 -2 -2 1 
    9 -3 3 -3 -1 
    10 -3 3 -3 -1 
    11 -3 3 -3 1 
    12 -3 3 -3 1 
    13 -4 -4 4 1 
    14 -4 -4 4 1 
    15 -4 -4 4 -1 
    16 -4 -4 4 -1 
    [,1] 
    1  1 
    2  1 
    3 -1 
    4 -1 
    5 -2 
    6 -2 
    7  2 
    8  2 
    9 -3 
    10 -3 
    11 3 
    12 3 
    13 4 
    14 4 
    15 -4 
    16 -4 
+0

当OP说“column1 * column2”我认为他想要列1和2的产品,但可能是我错了 – dickoa 2013-05-01 06:09:53

+0

@Stacy这不会做你的问题吗? – 2013-05-01 06:10:00

+0

谢谢@dickoa,我错过了最后一部分。编辑 – 2013-05-01 06:18:52

2

假设您的数据帧名为dat。如果我理解你的问题正确,你可以做以下

A1 <- as.matrix(dat[,c(1,2,2,3)] * dat[,c(4,3,4,4)]) 
A2 <- as.matrix(dat[,c(1,1,1,2)] * dat[,c(2,2,3,3)] * dat[,c(3,4,4,4)]) 
A3 <- as.matrix(dat[,1] * dat[,2] * dat[,3] * dat[,4])