R programming Missing value error

0 votes

I'm executing the following block of code:

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)
}
}
}

On running this code i get an error:

 "Error in if (mm[[j]] == mm[[j + 1]]) { : missing value where TRUE/FALSE needed "
Jan 24, 2019 in Data Analytics by Tyrion anex
• 8,700 points
921 views

1 answer to this question.

0 votes

You can achieve this by 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
answered Jan 24, 2019 by Sophie may
• 10,610 points

Related Questions In Data Analytics

0 votes
1 answer

R programming error: { : missing value where TRUE/FALSE needed "

This should work: for ( i in 1:dim(m)[[1]] ...READ MORE

answered May 28, 2019 in Data Analytics by Zulaikha
• 910 points
21,721 views
+4 votes
0 answers

R programming Error: Error in if (d >= 0) { : missing value where TRUE/FALSE needed

Here is my code: for(i in 1:(nrow(moon_sub))){   l_df[i,] <- ...READ MORE

Jun 24, 2020 in Data Analytics by Molly
• 160 points
789 views
+3 votes
2 answers

Error: could not find function - R Programming

Yes, Just like @Maverik said, It happens ...READ MORE

answered Aug 23, 2019 in Data Analytics by anonymous
• 33,030 points
32,929 views
0 votes
1 answer

Error saying "Error in rnorm() : argument "n" is missing, with no default" in R

Hey @ali, rnorm() function requires an argument, ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,840 points
3,035 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,442 views
0 votes
1 answer

How to remove certain character from a vector

We can use sub to remove the * by specifying fixed = ...READ MORE

answered Nov 14, 2018 in Data Analytics by Maverick
• 10,840 points
466 views
0 votes
1 answer

What are special values in R

Like most programming languages, R has a ...READ MORE

answered Nov 14, 2018 in Data Analytics by Maverick
• 10,840 points
2,672 views
0 votes
1 answer

What does the inf special character mean in R?

inf stands for infinity and only applies ...READ MORE

answered Nov 14, 2018 in Data Analytics by Maverick
• 10,840 points
2,452 views
0 votes
1 answer

R programming: Unexpected symbol error

Format your code this way: myfunction <- function() ...READ MORE

answered Dec 17, 2018 in Data Analytics by Sophie may
• 10,610 points
2,856 views
0 votes
1 answer

R Programming error in 'fert'

You're using a factor: fert <- factor(c(50,20,10,10,20,50)) levels(fert) #[1] ...READ MORE

answered Dec 28, 2018 in Data Analytics by Sophie may
• 10,610 points
465 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP