[R] missing TRUE/FALSE error in conditional construct

Moshe Olshansky m_olshansky at yahoo.com
Thu Aug 14 03:23:25 CEST 2008


The problem is that if x is either NA or NaN then x != 0 is NA (and not FALSE or TRUE) and the function is.nan tests for a NaN but not for NA, i.e. is.nan(NA) returns FALSE.

You can do something like:

mat_zeroless[!is.na(mat) & mat != 0] <- mat[!is.na(mat) & mat != 0]


--- On Thu, 14/8/08, rcoder <mpdotbook at gmail.com> wrote:

> From: rcoder <mpdotbook at gmail.com>
> Subject: [R]  missing TRUE/FALSE error in conditional construct
> To: r-help at r-project.org
> Received: Thursday, 14 August, 2008, 8:16 AM
> Hi everyone,
> 
> I posted something similar to this in reply to another
> post, but there seems
> to be a problem getting it onto the board, so I'm
> starting a new post.
> 
> I am trying to use conditional formatting to select
> non-zero and non-NaN
> values from a matrix and pass them into another matrix. The
> problem is that
> I keep encountering an error message indicating the
> ":missing value where
> TRUE/FALSE needed "
> 
> My code is as follows:
> 
> ##Code Start 
> mat_zeroless<-matrix(NA,5000,2000) #generating holding
> matrix 
> 
> ##Assume source matrix containing combination of values,
> NaNs and zeros##
> for (j in 1:5000) 
>         { 
> for (k in 1:2000) 
> { 
> if(mat[j,k]!=0 & !is.NaN(mat[j,k]))
> {mat_zeroless[j,k]<-mat[j,k]} 
> } 
>         } 
> ##Code End 
> 
> Error in if (mat[j,k] !=0 & !is.NaN(mat[j,k])) {
> :missing value where
> TRUE/FALSE needed 
> 
> I'm not sure how to resolve this.
> 
> rcoder 
> -- 
> View this message in context:
> http://www.nabble.com/missing-TRUE-FALSE-error-in-conditional-construct-tp18972244p18972244.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.



More information about the R-help mailing list