2013-05-01 36 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])