> j <- function() {+ if(!exists ("a")){ + a <- 1 + } else{ + a <- a+1 + } print(a)} Error: unexpected symbol in: "a <- a+1 } print"> j <- function() {+ if(!exists ("a")){ + a <- 1 + } else{ + a <- a+1 + } print("a")} Error: unexpected symbol in: "a <- a+1 } print" [[alternative HTML version deleted]]
Hello, I don't see the error you mention. j <- function() { if(!exists ("a")){ a <- 1 } else{ a <- a+1 } print(a) } j() [1] 1 sessionInfo() R version 3.3.2 (2016-10-31) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 locale: [1] LC_COLLATE=Portuguese_Portugal.1252 LC_CTYPE=Portuguese_Portugal.1252 [3] LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C [5] LC_TIME=Portuguese_Portugal.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_3.3.2 Rui Barradas Em 24-11-2016 07:43, Stuti Verma escreveu:>> j <- function() { > + if(!exists ("a")){ > + a <- 1 > + } else{ > + a <- a+1 > + } print(a)} > > Error: unexpected symbol in: > "a <- a+1 > } print" > >> j <- function() { > + if(!exists ("a")){ > + a <- 1 > + } else{ > + a <- a+1 > + } print("a")} > > Error: unexpected symbol in: > "a <- a+1 > } print" > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
`Hi Stuti, Your problem is that if you want to have more than one command on a single line, you must separate them with a semicolon. j <- function() { if(!exists ("a")){ a <- 1 } else{ a <- a+1 }; print(a)} The above will work, but is usually considered bad form. What follows is usually easier to read and avoids that sort of error. j <- function() { if(!exists ("a")) { a <- 1 } else { a <- a+1 } print(a) } Notice how the opening and closing braces ({}) are "lined up" to make the code easier to read. Most people recommend using at least two spaces indent, but far be it from me to demand conformity in these matters. Jim On Thu, Nov 24, 2016 at 6:43 PM, Stuti Verma <stuti.verma284 at gmail.com> wrote:>> j <- function() { > + if(!exists ("a")){ > + a <- 1 > + } else{ > + a <- a+1 > + } print(a)} > > Error: unexpected symbol in: > "a <- a+1 > } print" > >> j <- function() { > + if(!exists ("a")){ > + a <- 1 > + } else{ > + a <- a+1 > + } print("a")} > > Error: unexpected symbol in: > "a <- a+1 > } print" > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
If I was to make a totally wild leap, I would say it looks like someone using an inappropriate text editor such as Microsoft Word to edit R code. Stuti, please carefully read and follow the recommendations in the Posting Guide mentioned at the bottom of this and every post on this mailing list before posting again. R is a plain text language, and this list is a plain text mailing list. -- Sent from my phone. Please excuse my brevity. On November 24, 2016 12:44:11 PM PST, Rui Barradas <ruipbarradas at sapo.pt> wrote:>Hello, > >I don't see the error you mention. > >j <- function() { > if(!exists ("a")){ > a <- 1 > } else{ > a <- a+1 > } > print(a) >} > >j() >[1] 1 > > >sessionInfo() >R version 3.3.2 (2016-10-31) >Platform: x86_64-w64-mingw32/x64 (64-bit) >Running under: Windows 7 x64 (build 7601) Service Pack 1 > >locale: >[1] LC_COLLATE=Portuguese_Portugal.1252 >LC_CTYPE=Portuguese_Portugal.1252 >[3] LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C > >[5] LC_TIME=Portuguese_Portugal.1252 > >attached base packages: >[1] stats graphics grDevices utils datasets methods base > >loaded via a namespace (and not attached): >[1] tools_3.3.2 > >Rui Barradas > >Em 24-11-2016 07:43, Stuti Verma escreveu: >>> j <- function() { >> + if(!exists ("a")){ >> + a <- 1 >> + } else{ >> + a <- a+1 >> + } print(a)} >> >> Error: unexpected symbol in: >> "a <- a+1 >> } print" >> >>> j <- function() { >> + if(!exists ("a")){ >> + a <- 1 >> + } else{ >> + a <- a+1 >> + } print("a")} >> >> Error: unexpected symbol in: >> "a <- a+1 >> } print" >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at 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 at 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.