pearselab / r-intro-jenessalemon

r-intro-jenessalemon created by GitHub Classroom
0 stars 1 forks source link

lesson_2 #15 #11

Open jenessalemon opened 7 years ago

jenessalemon commented 7 years ago

@Wolflab @willpearse The code for this problem starts on line 348. I have had this problem before. When I put a Boolean in to an if statement:

if (distance[i] >= 5){ return(i) break }

I get an error saying:

Error in if (distance[i] >= 5) { : missing value where TRUE/FALSE needed

I thought that that must mean that distance is just full of NAs. However, I printed distance and got real values. Any ideas on how to fix this?

willpearse commented 7 years ago

Right, but did you print distance[i]? What does that look like? Try printing that out in your loop and seeing what the value of distance[i] looks like just before you get the error...

jenessalemon commented 7 years ago

Crap it's NA. How do I fix this? It doesn't make sense for the whole thing to be fine but one iteration to be NA? I will just try printing at each step and see when it becomes NA...

jenessalemon commented 7 years ago

It looks like the values go up and up until the last iteration, where they become NA. So I tried using if (distance[i-1] >= 5) and it worked...but that is probably not the best way to fix it!

jenessalemon commented 7 years ago

Actually that didn't fix it, it is telling me that the wanderer is never falling off the cliff, even though he is well past 5 mi away.

willpearse commented 7 years ago

Come online at https://appear.in/usubioprogramming