[R] More than on loop??

jim holtman jholtman at gmail.com
Mon Jan 25 01:59:06 CET 2010


Notice that 'nc' is multivalued (nc<-x$Label[61:308]).  If you want to
check if x$Label[i] is one of the values in 'nc', then use %in%:

if (x$Label[i] %in% nc) ....

The error happens because 'if' can only have a single conditional
expressions and your original 'if' statement would have resulted in
multiple logical values.  THe error message says that 'if' can only
use 1, so it picks the first and ignores the rest.  It is probably not
giving you the results you expect.

On Sun, Jan 24, 2010 at 6:14 PM, che <fadialnaji at live.com> wrote:
>
> Really thanks very much, with your help i was able to write a prober code to
> count the aminoacids in all the cleaved and noncleaved and then to display
> the results in a matrix with 8 column, i used only two loops instead of
> three. The code is working but i still have warning telling me that:
>  “In if (x$Label[i] == nc) { ... :
>  the condition has length > 1 and only the first element will be used)”
> So please can you help me with this warning what is the reason of it as i
> can’t understand exactly what does it mean?
> Here is the code that i am using, and the data file is attached:
> 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]==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]==c)
> {
> 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)
> }
>
> http://n4.nabble.com/file/n1288922/hiv.dat hiv.dat
> --
> View this message in context: http://n4.nabble.com/More-than-on-loop-tp1015851p1288922.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