Re: [R] unexpected 'else' in " else"
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] Richard O'Keefe's remarks on the workings of the interpreter are correct, but the code examples are ugly and hard to read. (On the other hand, anyone who has used the debugger may be de-sensitised to horrible code formatting.) The use of whitespace should if possible reflect the structure 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 " else" Message-ID: Content-Type: text/plain; charset="utf-8" ... The basic issue is that the top level wants to get started on your command AS SOON AS IT HAS A COMPLETE COMMAND, and if (...) stmt is complete. It's not going to hang around "Waiting for Godot" for an 'else' that might never ever ever turn up. So if (x < y) z <- x else z <- y is absolutely fine, no braces needed, while if (x < y) z <- x else z <- y will see the eager top level rush off to do your bidding at the end of the first line and then be completely baffled by an 'else' where it does not expect one. It's the same reason that you break AFTER infix operators instead of BEFORE. x <- y + z works fine, while x <- y + z doesn't. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] unexpected 'else' in " else"
Richard O'Keefe's remarks on the workings of the interpreter are correct, but the code examples are ugly and hard to read. (On the other hand, anyone who has used the debugger may be de-sensitised to horrible code formatting.) The use of whitespace should if possible reflect the structure 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 " else" Message-ID: Content-Type: text/plain; charset="utf-8" ... The basic issue is that the top level wants to get started on your command AS SOON AS IT HAS A COMPLETE COMMAND, and if (...) stmt is complete. It's not going to hang around "Waiting for Godot" for an 'else' that might never ever ever turn up. So if (x < y) z <- x else z <- y is absolutely fine, no braces needed, while if (x < y) z <- x else z <- y will see the eager top level rush off to do your bidding at the end of the first line and then be completely baffled by an 'else' where it does not expect one. It's the same reason that you break AFTER infix operators instead of BEFORE. x <- y + z works fine, while x <- y + z doesn't. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] unexpected 'else' in " else"
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 '}' and 'else' to avoid a syntax error in entering a 'if ... else' construct at the keyboard or via 'source'. For that reason, one (somewhat extreme) attitude of defensive programming is to always use braces, e.g., for 'if' clauses. The basic issue is that the top level wants to get started on your command AS SOON AS IT HAS A COMPLETE COMMAND, and if (...) stmt is complete. It's not going to hang around "Waiting for Godot" for an 'else' that might never ever ever turn up. So if (x < y) z <- x else z <- y is absolutely fine, no braces needed, while if (x < y) z <- x else z <- y will see the eager top level rush off to do your bidding at the end of the first line and then be completely baffled by an 'else' where it does not expect one. It's the same reason that you break AFTER infix operators instead of BEFORE. 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)) > + 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@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] unexpected 'else' in " else" (Ebert,Timothy Aaron)
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: z <- c(TRUE,TRUE,FALSE) > attributes(z) NULL ## so no 'class' among attributes(z) ## However > class(z) ## S3 class [1] "logical" ## Similarly > w <- ifelse(z,5,'a') > attributes(w) NULL ## so no 'class' among attributes(w) > class(w) ##S3 class [1] "character" So my (anyway) confusion stems from conflating the S3 'class' of the object with a "class" attribute, of which there is none. Nevertheless, I believe that the phrase I suggested (or something along those lines) might clarify how the S3 class is determined and perhaps better distinguish it from a "class" attribute among the attributes, if there there is such. Or maybe that part of the doc should just be removed. My guess is that this documentation has been around for a long time and no one has gotten around to revising it once S3 classes came into wider use. ... or saw the need to revise it, anyway. -- Bert " On Mon, Oct 24, 2022 at 9:30 AM Bert Gunter wrote: > > "...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 test and data values from the values of yes or no. **The > mode of the answer will be coerced from logical to accommodate first > any values taken from yes and then any values taken from no.** > > So the values are taken from 'yes' and 'no' (with coercion if they are > of different classes), and the class of the result will presumably be > inferred from the mode of those values. e.g. > > > z <- c(TRUE,TRUE,FALSE) > > class(z) > [1] "logical" > > w <- ifelse(z,5,'a') > > class(w) > [1] "character" ## note coercion > > So it would appear that the ifelse() documentation needs to be > clarified. For example, if the above asterisked phrase were "The S3 > *class* of the answer will be inferred from the mode, where the mode > of the answer will be coerced ..." that might resolve at least that > bit of confusion However, that might also be incorrect -- what about > S4 vs S3 vs Reference classes, for example (are such cases even > possible?)? I leave resolution of these matters -- or at least their > accurate and complete documentation -- to wiser heads. > > Cheers, > Bert > > > > > On Mon, Oct 24, 2022 at 8:45 AM Jorgen Harmse via R-help > wrote: > > > > 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 > > recycled as necessary in case `test` is longer. On top of that, there seems > > to be some sugar that suppresses evaluations in case `all(test)` and/or > > `all(!test)`, and the return type can be `logical` even if `yes` & `no` are > > not. I agreed with the other responses already, but my experiments further > > confirmed that `ifelse` is not interchangeable with `if() else > > `. > > > > > > > > The documentation confirms most of this, 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`. > > > > > > > > Regards, > > > > Jorgen Harmse. > > > > > > > > > ifelse(FALSE, {cat("Evaluating the vector for 'if'.\n"); 1:3}, > > > {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > > > Evaluating the vector for 'else'. > > > > [1] 0 > > > > > ifelse(rep(FALSE,5L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, > > > {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > > > Evaluating the vector for 'else'. > > > > [1] 0 1 2 3 4 > > > > > ifelse(rep(TRUE,3L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, > > > {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > > > Evaluating the vector for 'if'. > > > > [1] 1 2 3 > > > > > ifelse(c(TRUE,TRUE,FALSE), {cat("Evaluating the vector for 'if'.\n"); > > > 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > > > Evaluating the vector for 'if'. > > > > Evaluating the vector for 'else'. > > > > [1] 1 2 2 > > > > > ifelse(c(TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector for > > > 'if'.\n"); 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > > > Evaluating the vector for 'if'. > > > > Evaluating the vector for 'else'. > > > > [1] 1 2 2 1 > > > > > args(ifelse) > > > > function (test, yes, no) > > > > NULL > > > > > ifelse(c(TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the > > > vector for 'if'.\n");
Re: [R] unexpected 'else' in " else" (Ebert,Timothy Aaron)
"...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 test and data values from the values of yes or no. **The mode of the answer will be coerced from logical to accommodate first any values taken from yes and then any values taken from no.** So the values are taken from 'yes' and 'no' (with coercion if they are of different classes), and the class of the result will presumably be inferred from the mode of those values. e.g. > z <- c(TRUE,TRUE,FALSE) > class(z) [1] "logical" > w <- ifelse(z,5,'a') > class(w) [1] "character" ## note coercion So it would appear that the ifelse() documentation needs to be clarified. For example, if the above asterisked phrase were "The S3 *class* of the answer will be inferred from the mode, where the mode of the answer will be coerced ..." that might resolve at least that bit of confusion However, that might also be incorrect -- what about S4 vs S3 vs Reference classes, for example (are such cases even possible?)? I leave resolution of these matters -- or at least their accurate and complete documentation -- to wiser heads. Cheers, Bert On Mon, Oct 24, 2022 at 8:45 AM Jorgen Harmse via R-help wrote: > > 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 > recycled as necessary in case `test` is longer. On top of that, there seems > to be some sugar that suppresses evaluations in case `all(test)` and/or > `all(!test)`, and the return type can be `logical` even if `yes` & `no` are > not. I agreed with the other responses already, but my experiments further > confirmed that `ifelse` is not interchangeable with `if() else `. > > > > The documentation confirms most of this, 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`. > > > > Regards, > > Jorgen Harmse. > > > > > ifelse(FALSE, {cat("Evaluating the vector for 'if'.\n"); 1:3}, > > {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > Evaluating the vector for 'else'. > > [1] 0 > > > ifelse(rep(FALSE,5L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, > > {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > Evaluating the vector for 'else'. > > [1] 0 1 2 3 4 > > > ifelse(rep(TRUE,3L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, > > {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > Evaluating the vector for 'if'. > > [1] 1 2 3 > > > ifelse(c(TRUE,TRUE,FALSE), {cat("Evaluating the vector for 'if'.\n"); 1:3}, > > {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > Evaluating the vector for 'if'. > > Evaluating the vector for 'else'. > > [1] 1 2 2 > > > ifelse(c(TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector for 'if'.\n"); > > 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > Evaluating the vector for 'if'. > > Evaluating the vector for 'else'. > > [1] 1 2 2 1 > > > args(ifelse) > > function (test, yes, no) > > NULL > > > ifelse(c(TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector > > for 'if'.\n"); 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > Evaluating the vector for 'if'. > > Evaluating the vector for 'else'. > > [1] 1 2 2 1 2 0 1 > > > ifelse(logical(0L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, > > {cat("Evaluating the vector for 'else'.\n"); 0:4}) > > logical(0) > > > ifelse(TRUE, integer(0L), numeric(0L)) > > [1] NA > > > class(ifelse(TRUE, integer(0L), numeric(0L))) > > [1] "integer" > > > ifelse(integer(0L)) # test is an empty vector of integers and yes & no are > > missing. > > logical(0) > > > > > > > > > [[alternative HTML version deleted]] > > __ > R-help@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. __ R-help@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.
Re: [R] unexpected 'else' in " else" (Ebert,Timothy Aaron)
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 recycled as necessary in case `test` is longer. On top of that, there seems to be some sugar that suppresses evaluations in case `all(test)` and/or `all(!test)`, and the return type can be `logical` even if `yes` & `no` are not. I agreed with the other responses already, but my experiments further confirmed that `ifelse` is not interchangeable with `if() else `. The documentation confirms most of this, 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`. Regards, Jorgen Harmse. > ifelse(FALSE, {cat("Evaluating the vector for 'if'.\n"); 1:3}, > {cat("Evaluating the vector for 'else'.\n"); 0:4}) Evaluating the vector for 'else'. [1] 0 > ifelse(rep(FALSE,5L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, > {cat("Evaluating the vector for 'else'.\n"); 0:4}) Evaluating the vector for 'else'. [1] 0 1 2 3 4 > ifelse(rep(TRUE,3L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, > {cat("Evaluating the vector for 'else'.\n"); 0:4}) Evaluating the vector for 'if'. [1] 1 2 3 > ifelse(c(TRUE,TRUE,FALSE), {cat("Evaluating the vector for 'if'.\n"); 1:3}, > {cat("Evaluating the vector for 'else'.\n"); 0:4}) Evaluating the vector for 'if'. Evaluating the vector for 'else'. [1] 1 2 2 > ifelse(c(TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector for 'if'.\n"); > 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4}) Evaluating the vector for 'if'. Evaluating the vector for 'else'. [1] 1 2 2 1 > args(ifelse) function (test, yes, no) NULL > ifelse(c(TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector > for 'if'.\n"); 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4}) Evaluating the vector for 'if'. Evaluating the vector for 'else'. [1] 1 2 2 1 2 0 1 > ifelse(logical(0L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, > {cat("Evaluating the vector for 'else'.\n"); 0:4}) logical(0) > ifelse(TRUE, integer(0L), numeric(0L)) [1] NA > class(ifelse(TRUE, integer(0L), numeric(0L))) [1] "integer" > ifelse(integer(0L)) # test is an empty vector of integers and yes & no are > missing. logical(0) [[alternative HTML version deleted]] __ R-help@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.
Re: [R] unexpected 'else' in " else"
Your selection priorities are inverted: ifelse is vectorised... use it to make many individual choices at once with a single atomic vector result. If-else is a control-flow construct... you can return one object of any type from it based on a single comparison or alter variables and ignore the return value (or both, though that is probably not going to make for clear readability). Their use cases might appear to overlap if you choose to work with a length-one vector, but that is a somewhat obscure mis-use of ifelse. On October 21, 2022 10:38:48 AM PDT, "Ebert,Timothy Aaron" wrote: >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 >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 >>>>>> on Fri, 21 Oct 2022 12:05:58 + 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 Bis 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 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.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, 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@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-helpdata=05%7C01%7Ctebert%40ufl >>> .edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e >>> 1b84%7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w >>> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C >>> sdata=9ImIUx0YyKjFMTDRrwa5fnkqiJL9aDjpGCRxxfI6Hlk%3Dreserved >>> =0
Re: [R] unexpected 'else' in " else"
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 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 >>>>> on Fri, 21 Oct 2022 12:05:58 + 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 Bis 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 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.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, 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@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-helpdata=05%7C01%7Ctebert%40ufl >> .edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e >> 1b84%7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w >> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C >> sdata=9ImIUx0YyKjFMTDRrwa5fnkqiJL9aDjpGCRxxfI6Hlk%3Dreserved >> =0 >> PLEASE do read the posting guide >> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r%2Fdata=05%7C01%7Ctebert%40ufl.edu%7Cd3824be0a76a4e488dda08dab361d1e6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019529977132811%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=MTmSNg1tfjToCYtWtMf01Hhy93K0TpT3DPwuvYqVv0s%3Dreserved=0 >> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu% >> 7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84% >> 7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM >> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C >> sdata=WsqoNk2NQ6NOjKoOKwf%2FPU57XkAwKtRhw6xb68COT1o%3Dreserved=0 >> and provide commented, minimal, self-contained, reproducible code. >> > [[alternative HTML version deleted]] > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and m
Re: [R] unexpected 'else' in " else"
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 can try it yourself: local({ FILE <- tempfile(fileext = ".R") on.exit(unlink(FILE, force = TRUE, expand = FALSE), add = TRUE, after = FALSE) writeLines("if (TRUE) \n'evaluating cons.expr'\nelse 'evaluating alt.expr'", FILE) writeLines(readLines(FILE)) try(source(FILE, local = TRUE, echo = TRUE, verbose = FALSE)) }) If you try entering it as a function, it still fails: local({ FILE <- tempfile(fileext = ".R") on.exit(unlink(FILE, force = TRUE, expand = FALSE), add = TRUE, after = FALSE) writeLines("function () \nif (TRUE) \n'evaluating cons.expr'\nelse 'evaluating alt.expr'", FILE) writeLines(readLines(FILE)) try(source(FILE, local = TRUE, echo = TRUE, verbose = FALSE)) }) But R packages use sys.source() instead of source() to run R code, but it still fails if you run it: local({ FILE <- tempfile(fileext = ".R") on.exit(unlink(FILE, force = TRUE, expand = FALSE), add = TRUE, after = FALSE) writeLines("if (TRUE) \n'evaluating cons.expr'\nelse 'evaluating alt.expr'", FILE) writeLines(readLines(FILE)) try(sys.source(FILE, envir = environment())) }) The part that matters is that the function body is wrapped with braces. `if` statements inside braces or parenthesis (or possibly brackets) will continue looking for `else` even after `cons.expr` and a newline has been fully parsed, but will not otherwise. On Fri, Oct 21, 2022 at 10:39 AM Jorgen Harmse via R-help wrote: > > 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 > .GlobalEnv. There is some related sugar around packages that usually makes > things work the way a user would want.) At the command line, R executes code > whenever RETURN could be interpreted as the end of a statement. "If(….) …. > RETURN" is ambiguous: will it be followed by "else", or is it a complete > statement? If it's in a file or wrapped in a block or other structure that > obviously hasn't ended yet then R will wait to see the next line of input, > but if it could be a complete statement then not executing it would cause a > lot of frustration for users. Once the statement is executed, R expects > another statement, and no statement begins with "else". (Maybe the > interpreter could be enhanced to keep the "if" open under some conditions, > but I haven't thought it through. In particular, "if" without "else" is NULL > if the condition 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 > > > 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@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. > > > > [[alternative HTML version deleted]] > > > > > -- > > Subject: Digest Footer > > ___ > R-help@r-project.org mailing list > 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. > > > -- > > End of R-help Digest, Vol 236, Issue 19 > *** > >
Re: [R] unexpected 'else' in " else"
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 .GlobalEnv. There is some related sugar around packages that usually makes things work the way a user would want.) At the command line, R executes code whenever RETURN could be interpreted as the end of a statement. "If(�.) �. RETURN" is ambiguous: will it be followed by "else", or is it a complete statement? If it's in a file or wrapped in a block or other structure that obviously hasn't ended yet then R will wait to see the next line of input, but if it could be a complete statement then not executing it would cause a lot of frustration for users. Once the statement is executed, R expects another statement, and no statement begins with "else". (Maybe the interpreter could be enhanced to keep the "if" open under some conditions, but I haven't thought it through. In particular, "if" without "else" is NULL if the condition 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 > > 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@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. > [[alternative HTML version deleted]] -- Subject: Digest Footer ___ R-help@r-project.org mailing list 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. -- End of R-help Digest, Vol 236, Issue 19 *** [[alternative HTML version deleted]] __ R-help@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.
Re: [R] unexpected 'else' in " else"
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’ construct at the keyboard or via ‘source’. ``` Best, Jinsong On 2022/10/21 21:49, John Fox wrote: 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 code lines are in a function, the function body is enclosed in braces and so the interpreter sees the else clause. As I believe was already pointed out, you can similarly use braces at the command prompt to signal incompleteness of an expression, as in > {if (FALSE) print(1) + else print(2)} [1] 2 I hope this helps, John __ R-help@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.
Re: [R] unexpected 'else' in " else"
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 code lines are in a function, the function body is enclosed in braces and so the interpreter sees the else clause. As I believe was already pointed out, you can similarly use braces at the command prompt to signal incompleteness of an expression, as in > {if (FALSE) print(1) + else print(2)} [1] 2 I hope this helps, John -- John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada web: https://socialsciences.mcmaster.ca/jfox/ On 2022-10-21 8:06 a.m., Jinsong Zhao wrote: 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 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, 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@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 <http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. __ R-help@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. __ R-help@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.
Re: [R] unexpected 'else' in " else"
>>>>> Ebert,Timothy Aaron >>>>> on Fri, 21 Oct 2022 12:05:58 + 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 Bis 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 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.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, 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@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-helpdata=05%7C01%7Ctebert%40ufl >> .edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e >> 1b84%7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w >> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C >> sdata=9ImIUx0YyKjFMTDRrwa5fnkqiJL9aDjpGCRxxfI6Hlk%3Dreserved >> =0 >> PLEASE do read the posting guide >> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r >> -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu% >> 7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84% >> 7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM >> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C >> sdata=WsqoNk2NQ6NOjKoOKwf%2FPU57XkAwKtRhw6xb68COT1o%3Dreserved=0 >> and provide commented, minimal, self-contained, reproducible code. >> > [[alternative HTML version deleted]] > __ > R-help@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-helpdata=05%7C01%7Ctebert%40ufl.edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019419898095081%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=2mYpa25vpXNvPNINi9XxCtc7Vbj4Sp4IQUsA9fL%2BA60%3Dreserved=0 > PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019419898095081%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=21L
Re: [R] unexpected 'else' in " else"
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 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, 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@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 <http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. __ R-help@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.
Re: [R] unexpected 'else' in " else"
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 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.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, 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@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-helpdata=05%7C01%7Ctebert%40ufl > .edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e > 1b84%7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w > LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C > sdata=9ImIUx0YyKjFMTDRrwa5fnkqiJL9aDjpGCRxxfI6Hlk%3Dreserved > =0 > PLEASE do read the posting guide > https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r > -project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu% > 7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84% > 7C0%7C0%7C638019419897938843%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM > DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C > sdata=WsqoNk2NQ6NOjKoOKwf%2FPU57XkAwKtRhw6xb68COT1o%3Dreserved=0 > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@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-helpdata=05%7C01%7Ctebert%40ufl.edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019419898095081%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=2mYpa25vpXNvPNINi9XxCtc7Vbj4Sp4IQUsA9fL%2BA60%3Dreserved=0 PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.htmldata=05%7C01%7Ctebert%40ufl.edu%7C98bd495a0754455cbead08dab348311f%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638019419898095081%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7Csdata=21LO4e%2BNnGmlDjm3XfExMX63Z2viUePxMaHbcgm7JuA%3Dreserved=0 and provide commented, minimal, self-contained, reproducible code. __ R-help@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.
Re: [R] unexpected 'else' in " else"
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, 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@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. > [[alternative HTML version deleted]] __ R-help@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.
[R] unexpected 'else' in " else"
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@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.