简体   繁体   English

嵌套for循环,用于r中的矩阵运算

[英]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. 我有三个矩阵,其中需要3个嵌套的for循环才能进行所需的计算。 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 / bin / sh:BASH_FUNC_module():第0行:意外令牌附近的语法错误)' /bin/sh: BASH_FUNC_module(): line 0: BASH_FUNC_module()(){eval /usr/bin/modulecmd bash $* '/ bin / sh:错误导入“ BASH_FUNC_module”的函数定义coeff [z,j] * mod [i,z]中的错误:二进制运算符的非数字参数

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. 因此表mod包含非数字元素。 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]. 如果运行代码直到错误弹出并且一切都停止,您应该可以通过键入mod [i,z]获得该值。

(I am not sure about bash errors, they seem out of place and not related to R). (我不确定bash错误,它们似乎不合适并且与R无关)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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