简体   繁体   中英

nested for loop for matrix operations in r

I have three matrices where I need 3 nested for loops in order to do the calculations I need. My coeff matrix looks like:

lm.obj.coefficients lm.obj.coefficients.1 lm.obj.coefficients.2 lm.obj.coefficients.3     lm.obj.coefficients.4 lm.obj.coefficients.5 lm.obj.coefficien
4.552 0.61061 -0.06482 -0.22394 -0.08651 0.00306 -0.02061 -0.03066 2.35086 5.86607 0.92518 -8.1581     -0.21651 5.43852 -0.23804 -0.24025 -0.52292 -0.2
NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 
0.15725 0.13373 0.03761 -0.05429 0.0286 0.06517 0.05556 0.0515 0.01382 -0.06581 0.27052 1.06467 -0.01288 0.59487 0.02043 0.02361 -0.03473 0.01682 

Matrix resid looks like:

lm.obj.residuals lm.obj.residuals.1 lm.obj.residuals.2 lm.obj.residuals.3 lm.obj.residuals.4 lm.obj.residuals.5 lm.obj.residuals.6 lm.obj.residuals
0.00231 -1.89973 -0.0127 2.06897 -0.01311 -0.01215 -0.01229 -0.01237 -0.30846 -0.10754 0.06169 -0.18151 -0.0109 0.24604 -0.08252 -0.08839 0.02824 0
0.07514 0.18595 0.02237 -2.36732 0.02135 0.02571 0.02452 0.02403 0.06232 -0.05302 -0.0302 -0.09606 0.00519 -0.61982 -0.01513 -0.01797 0.01261 -0.00

and mod:

row.names       (Intercept) as.factor(tissue3)Gastrointestinal as.factor(tissue3)Hematopoietic as.factor(tissue3)Musculo_endo as.factor(Submission_
Adipose_Derived_1       1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 -0.0242845514406503 -0.28919947646494 0.164493834188179 -0.189891956939011 -0.01075313372
Adipose_Derived_92      1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 -0.0387879882628922 -0.266146134934971 0.125790966152757 -0.253593603953948 0.00062908003
Adipose_Derived_93      1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0.0856909184783238 -0.267523926775753 0.0601909297700556 -0.418856504832572 -0.0421275186

My code is:

coeff<-read.table("lm_coeff_rounded.txt",header=T,colClasses="numeric",nrows=31,comment.char="")

resid<-read.table("lm_resid_rounded.txt",header=T,colClasses="numeric",nrows=103,comment.char="")

mod<-read.table("mod.our",header=T)

coeff<-as.matrix(coeff)
resid<-as.matrix(resid)
mod<-as.matrix(mod)

coeff<-coeff[-2,]
mod<-mod[,-1]


cov.pred<-matrix(0,102,10)

for (j in 1:10){
  for (i in 1:102){
    for(z in 5:29){
      cov.pred[i,j]<-coeff[1,j] + resid[i,j] + (coeff[z,j]*mod[i,z])
    }
  }
}
write.table(cov.pred,"covariate_predicted_folds.txt",row.names=F,quote=F)

I am getting these errors:

/bin/sh: BASH_FUNC_module(): line 0: syntax error near unexpected token )' /bin/sh: BASH_FUNC_module(): line 0: BASH_FUNC_module() () { eval /usr/bin/modulecmd bash $* ' /bin/sh: error importing function definition for `BASH_FUNC_module' Error in coeff[z, j] * mod[i, z] : non-numeric argument to binary operator

Does anyone know what the problem is?

You can take a look at the error message:

Error in coeff[z, j] * mod[i, z] : non-numeric argument to binary operator

This says that you are computing a product with non-numeric arguments.

So the table mod has elements that are not numeric. If you run your code till the error pops up and everything stops you should be able to get that value by typing mod[i,z].

(I am not sure about bash errors, they seem out of place and not related to R).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM