'Getting output in combination form in r
I have 3 variables. Hence, I will get 6 combinations. I want to produce the 2nd column in conbination form. I got the folloWing result.
P<- matrix(c (
0.427, -0.193, 0.673,
-0.193, 0.094, -0.428,
0.673, -0.428, 224.099
), c( 3,3))
G <-matrix(c(
0.238, -0.033, 0.468,
-0.033, 0.084, -0.764,
0.468, -0.764, 205.144
), c(3,3))
A<-rep(1, each=nrow(P))
df<-do.call(rbind, lapply(1:ncol(P), function(x) {
do.call(rbind, combn(ncol(P), x, function(y) {
data.frame(comb = paste(y, collapse = ""),
B = ((solve(P)%*%G)%*%A),
stringsAsFactors = FALSE)
}, simplify = FALSE))
}))
>df
comb B
1 1 -19.7814149
2 1 -44.1515387
3 1 0.8891786
4 2 -19.7814149
5 2 -44.1515387
6 2 0.8891786
7 3 -19.7814149
8 3 -44.1515387
9 3 0.8891786
10 12 -19.7814149
11 12 -44.1515387
12 12 0.8891786
13 13 -19.7814149
14 13 -44.1515387
15 13 0.8891786
16 23 -19.7814149
17 23 -44.1515387
18 23 0.8891786
19 123 -19.7814149
20 123 -44.1515387
21 123 0.8891786
Here I got only 3 (-19.7814149,-44.1515387,0.8891786) values But I wanted 12 values.
comb B
1 0.5574
2 0.8936
3 0.9154
12 10.0772, 21.233
13 0.2083 , 0.9169
23 -3.1085, 0.9061
123 -19.7814, -44.1515, 0.8892
I can't manage this. Furthermore, I want to use these B values to calculate my desired result (GA), where my formula is
b<-t(B)
gain<-do.call(rbind, lapply(1:ncol(P), function(x) {
do.call(rbind, combn(ncol(P), x, function(y) {
data.frame(GA = abs(round(1.76*(sum(G[y,y] %*% B[y] * A[y]))/ sqrt((b[y] %*%P[y,y])%*%B[y]),2)),
stringsAsFactors = FALSE)
}, simplify = FALSE))
}))
My final output is
comb B GA
1 0.5574 0.641
2 0.8936 0.4822
3 0.9154 24.1186
12 10.0772, 21.233 3.123
13 0.2083 , 0.9169 24.1748
23 -3.1085, 0.9061 24.0867
123 -19.7814, -44.1515, 0.8892 24.9097
Is there any solution?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
