[R] unexpected 'else' in " else"

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Fri Oct 21 14:42:55 CEST 2022


>>>>> Ebert,Timothy Aaron 
>>>>>     on Fri, 21 Oct 2022 12:05:58 +0000 writes:

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

Note that this is *not* good advice:

  if(Cnd) A else B    is very much more efficient  than
  ifelse(Cnd, A, B)

whenever it is appropriate, i.e.,
the condition Cnd is a simple TRUE or FALSE.

ifelse() is very much over-used!

{For the more sophisticated reader:
 In R, these both are function calls:
 `if` is a function of 3 argument with a "peculiar" syntax and
 the third argument with default NULL.
}

Martin Maechler
ETH Zurich  and  R Core team



    > 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.

    > ______________________________________________
    > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
    > https://stat.ethz.ch/mailman/listinfo/r-help
    > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    > and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list