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"
[External Email
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 'else' in " e
.
x <- y +
z
works fine, while
x <- y
+ z
doesn't.
On Fri, 21 Oct 2022 at 22:29, Jinsong Zhao wrote:
> Hi there,
>
> The following code would cause R error:
>
> > w <- 1:5
> > r <- 1:5
> > if (is.matrix(r))
> +
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
"...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
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"
>
>[External Email]
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
>>>>> o
:29 Jinsong Zhao, wrote:
>
> > Hi there,
> >
> > The following code would cause R error:
> >
> > > w <- 1:5
> > > r <- 1:5
> > > if (is.matrix(r))
> > + r[w != 0, , drop = FALSE]
> > >
on is FALSE, so it might be necessary
to undo an assignment, and that seems very difficult.)
Regards,
Jorgen Harmse.
On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao, wrote:
> Hi there,
>
> The following code would cause R error:
>
> > w <- 1:5
> > r <- 1:5
> >
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’
gt; 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]
else r[w !=
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 " el
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:
---
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 to
if (is
[w != 0]
On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao, wrote:
> 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
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))
17 matches
Mail list logo