Question: Problem with a function iterating over a matrix in R
0
gravatar for Pia
6 months ago by
Pia10
Pia10 wrote:

I am trying to calculate my own substitution matrix from a multiple sequence alignment. In one of the steps, I need a function that will calculate the number of times each residue pair shows up and the total number of ungapped pair slots. The code is difficult to understand without context, but I think my problem is with syntax, so if anyone has any idea why it's returning 0, it would help a lot.

0 is the initial value of the variable, so I think my code is not even entering the loops for some reason

    ResiduePairCalculator = function(combination_alignmentmatrix, aa_pairs) 

  {

  totalpaircount=0
  for(i in 1:ncol(combination_alignmentmatrix))
  {
    list_1=strsplit(combination_alignmentmatrix[1,i],split="")
    list_2=strsplit(combination_alignmentmatrix[2,i],split="")

      for(j in 1:length(list_1))
      {
          if((list_1[[j]]!="-") && (list_2[[j]]!="-"))
           {
            totalpaircount=totalpaircount+1

              for(k in 1:ncol(aa_pairs))
              {
                pair=paste(aa_pairs[1,k] , aa_pairs[2,k], sep = "")
                match1=paste(list_1[[j]],list_2[[j]], sep = "")
                match2=paste(list_2[[j]],list_1[[j]], sep = "")

                  if((pair==match1) || (pair==match2))
                  {
                    aa_pairs[3,k]=aa_pairs[3,k]+1
                    break
                  }
              }

            }
      }

  }
  return(totalpaircount)
}
iteration error loop R • 122 views
ADD COMMENTlink written 6 months ago by Pia10

0 is the initial value of the variable, so I think my code is not even entering the loops for some reason

No, all you can say is the the incrementing line is probably not being run. No statement can be made about the loop with just the return value.

ADD REPLYlink written 6 months ago by _r_am30k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1199 users visited in the last hour