[R] unexpected 'else' in " else"
Ebert,Timothy Aaron
tebert @end|ng |rom u||@edu
Fri Oct 21 19:38:48 CEST 2022
Is there a place where you would use ifelse()? I used it here because it was short and there was no indication of lots of data. In the example I cannot tell the difference of a few hundred milliseconds execution time. Benchmarking code is important for larger problems.
Tim
-----Original Message-----
From: Martin Maechler <maechler using stat.math.ethz.ch>
Sent: Friday, October 21, 2022 8:43 AM
To: Ebert,Timothy Aaron <tebert using ufl.edu>
Cc: Andrew Simmons <akwsimmo using gmail.com>; Jinsong Zhao <jszhao using yeah.net>; R-help Mailing List <r-help using r-project.org>
Subject: Re: [R] unexpected 'else' in " else"
[External Email]
>>>>> 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%2F&data=05%7C01%7Ctebert%40ufl.edu%7Cd3824be0a76a4e488dda08dab361d1e6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019529977132811%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=MTmSNg1tfjToCYtWtMf01Hhy93K0TpT3DPwuvYqVv0s%3D&reserved=0
>> -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%7Cd3824be0a76a4e488dda08dab361d1e6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019529977132811%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=5omzPR7XzgdKZG6pr0Ro%2F6hplvltKLHdDvNkOUVv5qM%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%7Cd3824be0a76a4e488dda08dab361d1e6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019529977289047%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=T%2F7it0%2BBAopri6bKPLiS8TySi%2FrFejAuvZLOJkVKA2A%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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7Cd3824be0a76a4e488dda08dab361d1e6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019529977289047%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=OGKDKErNG%2FCYRbDMbsLYqaoILdw84WIxD2D0HUY6XE4%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%7Cd3824be0a76a4e488dda08dab361d1e6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019529977289047%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=T%2F7it0%2BBAopri6bKPLiS8TySi%2FrFejAuvZLOJkVKA2A%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list