drez
drez

Reputation: 5

"missing value where TRUE/FALSE needed" syntax error in R code

When i run the code below this is the error I get:

Error in if (mm[[j]] == mm[[j + 1]]) {:
missing value where TRUE/FALSE needed

Any ideas on how to fix this

m <- matrix(c(1:5,NA,7,7,NA),
nrow=3,ncol=3,byrow=T)
print(m)
for ( i in 1:dim(m)[[1]] ) {
mm <- sort(m[i,],na.last=c(NA,NA,T)[[i]])
for ( j in 1:(length(mm)-1) ) {
if ( mm[[j]]==mm[[j+1]] ) {
cat(i,j,mm[[j]],fill=T)
}
}
}

Upvotes: 0

Views: 357

Answers (1)

LAP
LAP

Reputation: 6685

Try wrapping isTRUE() around your if-condition:

for ( i in 1:dim(m)[[1]] ) {
  mm <- sort(m[i,],na.last=c(NA,NA,T)[[i]])
  for ( j in 1:(length(mm)-1) ) {
    if ( isTRUE(mm[[j]]==mm[[j+1]]) ) {
      cat(i,j,mm[[j]],fill=T)
    }
  }
}

Output:

3 1 7

Upvotes: 2

Related Questions