Re: [R] unexpected 'else' in " else"

2022-10-28 Thread Ebert,Timothy Aaron
I appreciate this thread on coding. My preference for reading is to have complete sentences. I can read this: { if (x On Behalf Of Jorgen Harmse via R-help Sent: Friday, October 28, 2022 10:39 AM To: r-help@r-project.org Subject: Re: [R] unexpected 'else' in " else" [Exte

Re: [R] unexpected 'else' in " else"

2022-10-28 Thread Jorgen Harmse via R-help
ructure of the code, and I would usually rather throw in a few extra delimiters than obscure the structure. Regards, Jorgen Harmse. Examples (best viewed in a real text editor so things line up): { if (x To: Jinsong Zhao Cc: "r-help@r-project.org" Subject: Re: [R] unexpected 'el

Re: [R] unexpected 'else' in " else"

2022-10-26 Thread Richard O'Keefe
This is explained in books about S and R. The first place to look is of course > ?"if" which says Note that it is a common mistake to forget to put braces ('{ .. }') around your statements, e.g., after 'if(..)' or 'for()'. In particular, you should not have a newline between '}'

Re: [R] unexpected 'else' in " else" (Ebert,Timothy Aaron)

2022-10-24 Thread Bert Gunter
I wanted to follow up. A more careful reading of the following: "A vector of the same length and attributes (including dimensions and "class") as test..." So the above **refers only to a "class" attribute that appears among the attributes of test and result**. Using my previous example, note that:

Re: [R] unexpected 'else' in " else" (Ebert,Timothy Aaron)

2022-10-24 Thread Bert Gunter
"...but 'same length and attributes (including dimensions and ‘"class"’) as ‘test’' looks wrong. The output seems to be `logical` or something related to the classes of `yes` & `no`." The documentation in fact says: "A vector of the same length and attributes (including dimensions and "class") as

Re: [R] unexpected 'else' in " else" (Ebert,Timothy Aaron)

2022-10-24 Thread Jorgen Harmse via R-help
There were several interesting points about `ifelse`. The usual behaviour seems to be that all three inputs are evaluated, and the entries of `yes` corresponding to `TRUE` in `test` are combined with the entries of `no` corresponding to `FALSE` in `test`. Moreover, `yes` & `no` seem to be recycl

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Jeff Newmiller
gt; >Tim > >-Original Message- >From: Martin Maechler >Sent: Friday, October 21, 2022 8:43 AM >To: Ebert,Timothy Aaron >Cc: Andrew Simmons ; Jinsong Zhao ; >R-help Mailing List >Subject: Re: [R] unexpected 'else' in " else" > >[Extern

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Ebert,Timothy Aaron
Message- From: Martin Maechler Sent: Friday, October 21, 2022 8:43 AM To: Ebert,Timothy Aaron Cc: Andrew Simmons ; Jinsong Zhao ; R-help Mailing List Subject: Re: [R] unexpected 'else' in " else" [External Email] >>>>> Ebert,Timothy Aaron >>>>

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Andrew Simmons
The code working inside stats::weighted.residuals has nothing to do with being evaluated in a different environment than globalenv() and has nothing to do with being inside a package. The reason the code works inside stats::weighted.residuals is because the function body is wrapped with braces. You

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Jorgen Harmse via R-help
Andrew Simmons is correct but doesn't explain why the code works in the package. This is one of only two differences I have found between running code at the command line and running it from a file. (The other difference is that code in a file is often executed in an environment other than .Glob

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Jinsong Zhao
Dear John, Thank you very much for the explanation. It cleared up my confusion about the syntax of "if ... else...", which in the help page of "if" said: ``` In particular, you should not have a newline between ‘}’ and ‘else’ to avoid a syntax error in entering a ‘if ... else’ constru

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread John Fox
Dear Jinsong, When you enter these code lines at the R command prompt, the interpreter evaluates an expression when it's syntactically complete, which occurs before it sees the else clause. The interpreter can't read your mind and know that an else clause will be entered on the next line. When

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Martin Maechler
the answer, but not 100% sure. > Tim > -Original Message- > From: R-help On Behalf Of Andrew Simmons > Sent: Friday, October 21, 2022 5:37 AM > To: Jinsong Zhao > Cc: R-help Mailing List > Subject: Re: [R] unexpected 'else' in

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Jinsong Zhao
Thanks a lot! I know the first and third way to correct the error. The second way seems make me know why the code is correct in the function stats::weighted.residuals. On 2022/10/21 17:36, Andrew Simmons wrote: The error comes from the expression not being wrapped with braces. You could chan

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Ebert,Timothy Aaron
From: R-help On Behalf Of Andrew Simmons Sent: Friday, October 21, 2022 5:37 AM To: Jinsong Zhao Cc: R-help Mailing List Subject: Re: [R] unexpected 'else' in " else" [External Email] The error comes from the expression not being wrapped with braces. You could change it

Re: [R] unexpected 'else' in " else"

2022-10-21 Thread Andrew Simmons
The error comes from the expression not being wrapped with braces. You could change it to if (is.matrix(r)) { r[w != 0, , drop = FALSE] } else r[w != 0] or { if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w != 0] } or if (is.matrix(r)) r[w != 0, , drop = FALSE] else r[w

[R] unexpected 'else' in " else"

2022-10-21 Thread Jinsong Zhao
Hi there, The following code would cause R error: > w <- 1:5 > r <- 1:5 > if (is.matrix(r)) + r[w != 0, , drop = FALSE] > else r[w != 0] Error: unexpected 'else' in "else" However, the code: if (is.matrix(r)) r[w != 0, , drop = FALSE]