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
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
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 '}'
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:
"...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
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
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
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
>>>>
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
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
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
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
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
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
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
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
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]
17 matches
Mail list logo