[R] More than on loop??

jim holtman jholtman at gmail.com
Sat Jan 30 21:22:11 CET 2010


It is not entirely clear what you are trying to do.  Can you explain
what the matrix that you are creating out of 'cleaved' represents?

"Tell me what you want to do; not how you want to do it".  It is hard
to follow code when you have not explained what it is doing.  THere
appear to be all kinds of "magic" numbers in the code.

Why do you 'return(y/113*100)'.  What does that mean?  There are
probably easier ways to do it if we understood exactly what you wanted
to do.  It sounds like once you can define the transformation to
create the matrix, barplot will probably work for you.

On Wed, Jan 27, 2010 at 9:04 PM, che <fadialnaji at live.com> wrote:
>
>  yes, but the outcome graphs are almost the same, that mean it does not
> calculated in a cumulative way , if you apply the following code, then run
> hi(x), and then recta(x), you will see how the shape are similar to the
> frequency of Amino Acid  in the matrix. i am looking for a code that can do
> this automatically starting from the first column ending with the last
> column- data attached. many thanx
> x<-read.table("C:/Uni/502/CA2/hiv.dat", header=TRUE)
> num<-nrow(x)
> AA<-c('A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y')
> nc<-x$Label[61:308]
> c<-x$Label[nc]
> noncleaved<-function(x)
> {
> y<-matrix(0,20,8)
> colnames(y)<-c("N4","N3","N2","N1","C1","C2","C3","C4")
> for(i in 1:num){
> if (x$Label[i] %in% nc)
> {
> for(j in 1:8){
> res<-which(AA==substr(x$Peptide[i],j,j))
> y[res, j]=y[res, j]+1
> }
> }
> }
> return (y/274*100)
> }
>
> cleaved<-function(x)
> {
> y<-matrix(0,20,8)
> colnames(y)<-c("N4","N3","N2","N1","C1","C2","C3","C4")
> for(i in 1:num){
> if (x$Label[i] %in% nc)
> {
> for(j in 1:8){
> res<-which(AA==substr(x$Peptide[i],j,j))
> y[res, j]=y[res, j]+1
> }
> }
> }
> return (y/113*100)
> }
>
> hi<-function(x)
> {
> height<-rep(0,8)
> for (j in 1:8){
> height[j]<-sum(round(cleaved(x)[,j]))
> max.height<-max(height)
> }
> plot(c(0,10*8),c(0,max.height+20),col="white")
> }
> suma<-function(i,j,A)
> {
>  if( j<= 0)
>    {
>    sum<-0
>    }
>  else
>    {
>     sum<-0
>     for(k in 1:j)
>       {
>        sum<- sum + round(A[k,i])
>
>        }
>     }
> return(sum)
> }
>
> grafica<- function(A)
> {
> for(i in 1:8)
>  {
>  for(j in 1:20)
>    {
>    rect((i-1)*10,suma(i,j-1,A),((i-1)*10)+10,suma(i,j,A), col=colmap[j])
>    if ( A[j,i] != 0)
>      {
>       text( ((i-1)*10)+5, (suma(i,j-1,A) + round(A[j,i])/2), amino.acid[j],
> cex=( (2*round(A[j,i])/round(max(A))
> )))
>       }
>     }
>  }
> }
> recta<-function(x)
> {
> colmap<-c("#FFFFFF", "#FFFFCC", "#FFFF99", "#FFFF66", "#FFFF33",
> "#FFFF00", "#FFCCFF", "#FFCCCC", "#FFCC99", "#FFCC66", "#FFCC33",
> "#FFCC00", "#FF99FF", "#FF99CC", "#FF9999", "#FF9966", "#FF9933",
> "#FF9900", "#FF33FF", "#FF33CC")
>
> rect(1*10,20,10+10,20+round(cleaved(x)[1,1]),col=colmap[1])
> rect(1*10,40,10+10,40+round(cleaved(x)[2,1]),col=colmap[2])
> rect(1*10,53,10+10,53+round(cleaved(x)[3,1]),col=colmap[3])
> rect(1*10,63,10+10,63+round(cleaved(x)[4,1]),col=colmap[4])
> rect(1*10,69,10+10,69+round(cleaved(x)[5,1]),col=colmap[5])
> rect(1*10,73,10+10,73+round(cleaved(x)[6,1]),col=colmap[6])
> rect(1*10,85,10+10,85+round(cleaved(x)[7,1]),col=colmap[7])
> rect(1*10,89,10+10,89+round(cleaved(x)[8,1]),col=colmap[8])
> rect(1*10,96,10+10,96+round(cleaved(x)[9,1]),col=colmap[9])
> rect(1*10,110,10+10,110+round(cleaved(x)[10,1]),col=colmap[10])
> rect(1*10,118,10+10,118+round(cleaved(x)[11,1]),col=colmap[11])
> rect(1*10,123,10+10,123+round(cleaved(x)[12,1]),col=colmap[12])
> rect(1*10,144,10+10,144+round(cleaved(x)[13,1]),col=colmap[13])
> rect(1*10,149,10+10,149+round(cleaved(x)[14,1]),col=colmap[14])
> rect(1*10,158,10+10,158+round(cleaved(x)[15,1]),col=colmap[15])
> rect(1*10,170,10+10,170+round(cleaved(x)[16,1]),col=colmap[16])
> rect(1*10,198,10+10,198+round(cleaved(x)[17,1]),col=colmap[17])
> rect(1*10,213,10+10,213+round(cleaved(x)[18,1]),col=colmap[18])
> rect(1*10,225,10+10,225+round(cleaved(x)[19,1]),col=colmap[19])
> rect(1*10,229,10+10,225+round(cleaved(x)[20,1]),col=colmap[20])
>
> }
> http://n4.nabble.com/file/n1312372/hiv.dat hiv.dat
> http://n4.nabble.com/file/n1312372/hiv.txt hiv.txt
>
> --
> View this message in context: http://n4.nabble.com/More-than-on-loop-tp1015851p1312372.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list