[R] missing TRUE/FALSE error in conditional construct
Steven McKinney
smckinney at bccrc.ca
Thu Aug 14 03:13:59 CEST 2008
> -----Original Message-----
> From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org]
> On Behalf Of rcoder
> Sent: Wednesday, August 13, 2008 3:17 PM
> To: r-help at r-project.org
> Subject: [R] missing TRUE/FALSE error in conditional construct
>
>
> 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.
This seems to do what you appear to need,
no loops required (always best whenever possible):
> set.seed(123)
> mat <- matrix(sample(1:10), nrow = 5)
> mat
[,1] [,2]
[1,] 3 1
[2,] 8 10
[3,] 4 9
[4,] 7 2
[5,] 6 5
> is.na(mat) <- c(2, 3)
> mat
[,1] [,2]
[1,] 3 1
[2,] NA 10
[3,] NA 9
[4,] 7 2
[5,] 6 5
> mat[5,] <- 0
> mat
[,1] [,2]
[1,] 3 1
[2,] NA 10
[3,] NA 9
[4,] 7 2
[5,] 0 0
> matno0 <- matrix(NA, nrow = nrow(mat), ncol = ncol(mat))
> matno0
[,1] [,2]
[1,] NA NA
[2,] NA NA
[3,] NA NA
[4,] NA NA
[5,] NA NA
> mat[!is.na(mat) & !(mat == 0)]
[1] 3 7 1 10 9 2
> matno0[!is.na(mat) & !(mat == 0)] <- mat[!is.na(mat) & !(mat == 0)]
> matno0
[,1] [,2]
[1,] 3 1
[2,] NA 10
[3,] NA 9
[4,] 7 2
[5,] NA NA
>
Other issues:
The error messages you are seeing generally
are happening because you are generating
non-scalar TRUE / FALSE outcomes or
NA outcomes in your if() clause.
An if() clause should generate scalar
TRUE or FALSE only.
To this end, the S language provides
the operators '&&' (scalar AND)
and '||' (scalar OR) for use in
logical scalar clauses. Further,
if the first argument to '&&' tests
FALSE, the second is not even evaluated,
so you could have done
if ( !is.na(mat[j, k]) && mat[j, k] != 0 ) { blah }
In this case, once the NA is found, the if() clause
evaluation is over and you don't have to worry about
the NA that will result from the second argument
mat[j, k] != 0
> matno0 <- matrix(NA, nrow = nrow(mat), ncol = ncol(mat))
> matno0
[,1] [,2]
[1,] NA NA
[2,] NA NA
[3,] NA NA
[4,] NA NA
[5,] NA NA
> for ( j in 1:5 ) {
+ for ( k in 1:2 ) {
+ if ( !is.na(mat[j, k]) && mat[j, k] != 0 ) { matno0[j, k] <- mat[j,
k] }
+ }
+ }
>
> matno0
[,1] [,2]
[1,] 3 1
[2,] NA 10
[3,] NA 9
[4,] 7 2
[5,] NA NA
>
HTH
Steve McKinney
>
> 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