I have a matrix of 3 columns and 56 rows. Now I want to perform an operation on each of the (i,j) elements in this matrix and update the value in another pre-defined matrix of 3 columns and 56 rows.
A little snippet of my data is
miss_geno_probs
[,1] [,2] [,3]
[1,] 2.186745e-02 2.936486e-02 9.996548e-03
[2,] 3.131395e-02 4.205017e-02 1.431495e-02
[3,] 3.253137e-02 4.368498e-02 1.487148e-02
[4,] 3.158590e-02 4.241535e-02 1.443927e-02
[5,] 1.240662e-02 1.666031e-02 5.671596e-03
[6,] 3.447492e-02 4.629489e-02 1.575996e-02
I want to perform a simple operation on each of the elements thus:
w1 <- miss_geno_probs[1,1]/sum(miss_geno_probs[1,])
> w1
[1] 0.3571429
This is what I tried but it wouldn't give me my desired output:
missing_geno_weights<- matrix(rep(0, length(y_mis) * 3), ncol = 3)
miss_geno_probs<- matrix(rep(0, length(y_mis) * 3), ncol = 3)
for (i in seq_along(marginals)) {
for (j in 0:2){
miss_geno_probs[i,j+1]<- marginals[i] * geno_prop[j+1]
missing_geno_weights[i,j+1]<- miss_geno_probs[i,j+1]/sum(miss_geno_probs[i,])
}
}
After trying agin, I have broken this down into 2 for loops and the computation is correct for the first line but only computing for the first row.
missing_geno_weights<- matrix(rep(0, length(marginals) * 3), ncol = 3)
> for (i in 1:length(marginals)){
for (j in seq_along(1:3)){
missing_geno_weights[i, j]<- miss_geno_probs[i,j]/sum(miss_geno_probs[i,])
}
}
> missing_geno_weights
[,1] [,2] [,3]
[1,] 0.3571429 0.4795918 0.1632653
[2,] 0.3571429 0.4795918 0.1632653
[3,] 0.3571429 0.4795918 0.1632653
[4,] 0.3571429 0.4795918 0.1632653
[5,] 0.3571429 0.4795918 0.1632653