[R] Errors in if statement

arun smartpink111 at yahoo.com
Sat Sep 29 05:38:13 CEST 2012


Hi Z,

I guess it should work.
The apply(geno1,1,...)  applies on rows 1:nrow(geno1).  In the example dataset, there were 5 rows, the result vector (tmp) is also of length 5.   
A.K.






________________________________
From: JiangZhengyu <zhyjiang2006 at hotmail.com>
To: smartpink111 at yahoo.com 
Sent: Friday, September 28, 2012 10:55 PM
Subject: RE: [R] Errors in if statement



Thanks, Arun. The matrix format & put "NA" with 0 help.  but how do I do all the rows. Is the loop still OK?
Z


> Date: Fri, 28 Sep 2012 18:13:39 -0700
> From: smartpink111 at yahoo.com
> Subject: Re: [R] Errors in if statement
> To: zhyjiang2006 at hotmail.com
> CC: r-help at r-project.org
> 
> Hi,
> Try this:
> geno<-read.table(text="
> P1  P2  P3  P4 P5
> 1  2  2  3 2   
> 2  2  2  1 1
> 1  2  1  2  NA
> NA 2  3  4  5
> 1  1  3  1   3
> ",sep="",header=TRUE,stringsAsFactors=FALSE)
>  geno1<-as.matrix(geno)
>  geno1[is.na(geno1)]<-0
>  tmp<-apply(geno1,1,function(x) ifelse((sum(x!=2)>3) & (sum(x==1)>=1) & (sum(x==3)>=1), 1,0) )
> tmp
> #[1] 0 0 0 0 1
> A.K.
> 
> 
> 
> 
> ----- Original Message -----
> From: JiangZhengyu <zhyjiang2006 at hotmail.com>
> To: 
> Cc: r-help at r-project.org
> Sent: Friday, September 28, 2012 4:16 PM
> Subject: [R] Errors in if statement
> 
> 
> Hi guys, I have many rows (>1000) and columns (>30) of "geno" matrix. I use the following loop and condition statement (adapted from someone else code). I always have an error below.  I was wondering if anyone knows what's the problem & how to fix it.  
> Thanks,Zhengyu  ########### geno matrix P1  P2  P3  P4 
> 1  2  2  3 2    
> 2  2  2  1 1
> 1  2  1  2  NANA 2  3  4  5 ###########
> for(i in 1:4) {
> cat(i,"")
> if(sum(geno[i,]!=2)>3 && sum(geno[i,]==1)>=1 && sum(geno[i,]==3)>=1){
>    tmp = 1
>    }
> } ########### 1 2 Error in if (sum(geno[i, ] != 2) > 3 && sum(geno[i, ] == 1) >= 1 && sum(geno[i,  : 
>   missing value where TRUE/FALSE needed
>                                  
>     [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
>  




More information about the R-help mailing list