[R] for loop implementation in below problem

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Sun Mar 21 22:14:43 CET 2021


Hi Goyani,
In its present form, the function stalls because you haven't defined
pmat before trying to pass it to the function. gmat and wmat suffered
the same fate. Even if I define these matrices as I think you have,
"solve" fails because at least one is singular. First, put the
function in order as below. I think this is what you sent made
readable.

selection.index<- function(ID, phen_mat, gen_mat, weight_mat, GAY){
 ID<-toString(ID)
 p<-as.matrix(phen_mat)
 g<-as.matrix(gen_mat)
 w<-as.matrix(weight_mat)
 bmat<- solve(phen_mat) %*% gen_mat %*% weight_mat
 GA<- 2.063 * t(bmat) %*% g %*% w / (t(bmat) %*% p %*% bmat)^0.5
 if(missing(GAY)) PRE<-(GA/GA) * 100
 else PRE<-(GA/GAY) * 100
 result<-list(ID=ID,b=matrix(round(bmat,4),nrow=1),
  GA=round(GA,4),PRE=round(PRE,4))
 return(data.frame(result))
}

Next, what sort of matrices do you want to pass? Then an answer may emerge.

Jim

On Mon, Mar 22, 2021 at 6:03 AM Goyani Zankrut <zankrut20 using gmail.com> wrote:
>
> I created custom function according to my requirement which is given below:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *selection.index<- function(ID, phen_mat, gen_mat, weight_mat, GAY){  ID =
> toString(ID)  p<- as.matrix(phen_mat)  g<- as.matrix(gen_mat)  w<-
> as.matrix(weight_mat)  bmat<- solve(phen_mat) %*% gen_mat %*% weight_mat
> GA<- 2.063 * t(bmat) %*% g %*% w / (t(bmat) %*% p %*% bmat)^0.5  PRE<-
> if(missing(GAY)){    (GA/GA) * 100  } else {    (GA/GAY) * 100  }  result<-
> list("ID" = ID, "b" = matrix(round(bmat,4), nrow = 1), "GA" = round(GA,4),
> "PRE" = round(PRE,4))  return(data.frame(result))}*
>
> *sc<- list()*
> *sc[[1]]<- selection.index(ID = 12, pmat[1:2,1:2], gmat[1:2,1:2],
> wmat[1:2,1])*
> *sc[[2]]<- selection.index(ID = 13, pmat[c(1,3),c(1,3)],
> gmat[c(1,3),c(1,3)], wmat[c(1,3),1])*
>
> for more detail about question follow stack overflow link:
> https://stackoverflow.com/questions/66734928/how-to-solve-this-through-loop
> *"Healthy soil, Healthy life."*
> *"A war based on Satyagraha is always of two kinds. One is the war we wage
> against injustice, and the other we fight our won weaknesses."* - *Sardar
> Patel*
> *"You have to dream before your dreams can come true."* - *A. P. J.* *Abdul
> Kalam*
> *"Think before you print and save a tree."*
>
> *ZANKRUT GOYANI*
> *B.Sc. (Hons.) Agriculture*
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list