pearselab / r-intro-aspri951

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

Using the output of a prior function in a loop #2

Open aspri951 opened 7 years ago

aspri951 commented 7 years ago

As we discussed, I modified my prime function to return a logical statement rather than print a character string (see lines 152-200 of lesson 2). I then tried to use this output (TRUE/FALSE) in a loop.

What I tried to code: for all integers i in the range 1:20, if the function prime(i) yields the output "TRUE" (in other words if the output of prime(i) equates to "TRUE"), then print the character string "JOB."

Not what question #4 asks for, but I was trying to get a simpler version working first. I get this error:

Error in if (n%%i == 0) { : missing value where TRUE/FALSE needed

GUESS 1: R found == 0 where it was expecting ==TRUE/FALSE. Checked to see if setting prime(i) equal to a logical is actually valid (line 181). Appears to be valid. GUESS 2: R doesn't like that I used i within the function AND within the loop (same variable name used in two different circumstances). Changed i to j in my loop. Same error. GUESS 3: R doesn't like what comes after the curly bracket. Sees "return" where it expected "TRUE." Removed "return" from my prime function (line 185). Same error.

My question: What syntax rules do I lack/misunderstand that would allow me to understand this error?

willpearse commented 7 years ago

R expects either TRUE or FALSE inside the brackets of your if statement. You've given it something that doesn't evaluate to that. Try running each individual part of the thing inside the brackets to see what's going wrong. Remember that, like in maths, the order of operators matters. Thus n3+1 is not the same as n(3+1). See if that helps...

On Thu, Jan 19, 2017, 9:57 PM aspri951 notifications@github.com wrote:

As we discussed, I modified my prime function to return a logical statement rather than print a character string (see lines 152-200 of lesson 2). I then tried to use this output (TRUE/FALSE) in a loop.

What I tried to code: for all integers i in the range 1:20, if the function prime(i) yields the output "TRUE" (in other words if the output of prime(i) equates to "TRUE"), then print the character string "JOB."

Not what question #4 asks for, but I was trying to get a simpler version working first. I get this error:

Error in if (n%%i == 0) { : missing value where TRUE/FALSE needed

GUESS 1: R found == 0 where it was expecting ==TRUE/FALSE. Checked to see if setting prime(i) equal to a logical is actually valid (line 181). Appears to be valid. GUESS 2: R doesn't like that I used i within the function AND within the loop (same variable name used in two different circumstances). Changed i to j in my loop. Same error. GUESS 3: R doesn't like what comes after the curly bracket. Sees "return" where it expected "TRUE." Removed "return" from my prime function (line 185). Same error.

My question: What syntax rules do I lack/misunderstand that would allow me to understand this error?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pearselab/r-intro-aspri951/issues/2, or mute the thread https://github.com/notifications/unsubscribe-auth/ABLcUpc6PqlPSRz2FkFKSDXRosezEzH0ks5rUD7AgaJpZM4Lo7bA .

aspri951 commented 7 years ago

Could you clarify what you mean by running each part of what's inside the brackets alone? I'm not sure I know how to do this! However, I re-wrote my prime function (again), and it works as it's supposed to:

prime <- function(n){
  if (n == 2){return(TRUE)} else {
    for (i in (n-1):2){
      if(n %% i == 0){return(FALSE)
        break}
    }
    for (i in (n-1):2){
      if(n %% i != 0){return(TRUE)}
    }
  }
}

I also re-wrote my baby-loop in a way that makes more intuitive sense to me, but I am still getting the same error (Error in if (n%%i == 0) { : missing value where TRUE/FALSE needed). Baby loop:

for (i in 1:20){
  if (prime(i) == TRUE){
    print("JOB")
  }
}

I'm guessing I could work around this error by actually integrating the code from my prime function into my loop, but I would really rather understand why this isn't working. I have also tried re-writing my prime function without "return" such that my if-statements have literally nothing but TRUE/FALSE inside the brackets (if(n == 2){TRUE} etc.), but I still receive the same error. I am now at a loss as to how to proceed...

P.S. I just figured out that I can use Markdown stuff on GitHub.

willpearse commented 7 years ago

Glad you're making progress. I think I went through done debugging techniques in the lectures, but it's good to review them now as they will help you find your error.

When you get an error a loop, the loop index still had the value it had when you got the error. So if your loop have an error on the third iteration, i would be equal to 3. Check what your iterator value is, then see if that helps you debug your problem.

What I meant was you can run each part of the code separately. So "print(I)" could be entered into the console, or run, as "I" or "print (I)". The same goes for your statement in the if. However, you need what I wrote in the paragraph above to solve your problem.

Lastly, if you "return" from a function, there's no need for a "break". R (unlike some languages) will just return straight away for you.

On Sat, Jan 21, 2017, 12:03 AM aspri951 notifications@github.com wrote:

Could you clarify what you mean by running each part of what's inside the brackets alone? I'm not sure I know how to do this! However, I re-wrote my prime function (again), and it works as it's supposed to:

prime <- function(n){ if (n == 2){return(TRUE)} else { for (i in (n-1):2){ if(n %% i == 0){return(FALSE) break} } for (i in (n-1):2){ if(n %% i != 0){return(TRUE)} } } }

I also re-wrote my baby-loop in a way that makes more intuitive sense to me, but I am still getting the same error (Error in if (n%%i == 0) { : missing value where TRUE/FALSE needed). Baby loop:

for (i in 1:20){ if (prime(i) == TRUE){ print("JOB") } }

I'm guessing I could work around this error by actually integrating the code from my prime function into my loop, but I would really rather understand why this isn't working. I have also tried re-writing my prime function without "return" such that my if-statements have literally nothing but TRUE/FALSE inside the brackets (if(n == 2){TRUE} etc.), but I still receive the same error. I am now at a loss as to how to proceed...

P.S. I just figured out that I can use Markdown stuff on GitHub.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/pearselab/r-intro-aspri951/issues/2#issuecomment-274236453, or mute the thread https://github.com/notifications/unsubscribe-auth/ABLcUj-dhnQnzyU4-pZYDqCK_-nLOQRhks5rUZGugaJpZM4Lo7bA .