[R] unexpected 'else' in " else"

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Fri Oct 21 14:05:58 CEST 2022


I can get it to work with
ifelse(is.matrix(r), r[w!=0, , drop=FALSE], r[w!=0])

With w and r as defined r is not a  matrix, so the first part will never execute. The test is for w not equal to zero so it is always true for these vectors. It is usually good to have test code such that all possible cases are activated.

r<--1:8
w<- -1:5
if(is.matrix(r)){
  r[w!=0, , drop=FALSE]
} else r[w != 0]

I think this also works. The "else" must follow the } and you put in a carriage return after the }

I think that is the answer, but not 100% sure.

Tim

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Andrew Simmons
Sent: Friday, October 21, 2022 5:37 AM
To: Jinsong Zhao <jszhao using yeah.net>
Cc: R-help Mailing List <r-help using r-project.org>
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.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 != 0]


On Fri., Oct. 21, 2022, 05:29 Jinsong Zhao, <jszhao using yeah.net> 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]
> Error: unexpected 'else' in "        else"
>
> However, the code:
>          if (is.matrix(r))
>              r[w != 0, , drop = FALSE]
>          else r[w != 0]
> is extracted from stats::weighted.residuals.
>
> My question is why the code in the function does not cause error?
>
> Best,
> Jinsong
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl
> .edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> &sdata=9ImIUx0YyKjFMTDRrwa5fnkqiJL9aDjpGCRxxfI6Hlk%3D&reserved
> =0
> PLEASE do read the posting guide
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%
> 7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&
> sdata=WsqoNk2NQ6NOjKoOKwf%2FPU57XkAwKtRhw6xb68COT1o%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019419898095081%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=2mYpa25vpXNvPNINi9XxCtc7Vbj4Sp4IQUsA9fL%2BA60%3D&reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019419898095081%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=21LO4e%2BNnGmlDjm3XfExMX63Z2viUePxMaHbcgm7JuA%3D&reserved=0
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list