Hi, I am new to R. I was trying to get a very simple program to run. Take one number from the command line. If the number < 0 return -1. If number > 0 return 1 and if the number == 0 return 0. The code is in a file called test1.R The code: #useage: R --no-save --args 5 < test1.R args = (commandArgs(TRUE)) x = as.numeric(args[1]) print(x) res <- conditional1(x) cat("result= ",res,"\n") conditional1 <- function(x){ result <- 0 if (x > 0) { result <- 1 } else if (x < 0) { result <- -1 } return(result) } The output:>R --no-save --slave --args 1 < test1.R[1] 1 result= 1>R --no-save --slave --args -1 < test1.R[1] -1 result= -1>] R --no-save --slave --args 0 < test1.R[1] 0 result= -1 The problem: For arguments 1 and -1 it works as intended. For 0 (zero) it does not. If the 0 value is passed into the function named "conditional1" I would expect both if-statements to evaluate to false and 0 being return. From what I can tell (0 < 0) evaluates to true since -1 is returned. Hmmmmm... What is going on? What am I doing wrong? Why is this happening? I am baffled! Any help would be appreciated. Thanks Mike [[alternative HTML version deleted]]
Seems to work fine for me:> conditional1 <- function(x){+ result <- 0 + if (x > 0) { + result <- 1 + } else if (x < 0) { + result <- -1 + } + return(result) + }> conditional1(-1)[1] -1> conditional1(1)[1] 1> conditional1(0)[1] 0>On Mon, Feb 13, 2012 at 4:59 PM, Schmidt, Michael <MSchmidt at med.miami.edu> wrote:> Hi, > I am new to R. I was trying to get a very simple program to run. Take one number from the command line. If the number < 0 return -1. If number > 0 return 1 and if the number == 0 return 0. The code is in a file called test1.R > > > The code: > > #useage: R --no-save --args 5 < test1.R > args = (commandArgs(TRUE)) > x = as.numeric(args[1]) > print(x) > > res <- conditional1(x) > cat("result= ",res,"\n") > > conditional1 <- function(x){ > ? ? ? ?result <- 0 > ? ? ? ?if (x > 0) { > ? ? ? ? ? ? ? ?result <- 1 > ? ? ? ?} else if (x < 0) { > ? ? ? ? ? ? ? ?result <- -1 > ? ? ? ?} > ? ? ? ?return(result) > } > > > The output: >>R --no-save --slave --args 1 < test1.R > [1] 1 > result= ?1 >>R --no-save --slave --args -1 < test1.R > [1] -1 > result= ?-1 >>] R --no-save --slave --args 0 < test1.R > [1] 0 > result= ?-1 > > > The problem: > For arguments 1 and -1 it works as intended. For 0 (zero) it does not. If the 0 value is passed into the function named "conditional1" ?I would expect both if-statements to evaluate to false and 0 being return. From what I can tell (0 < 0) evaluates to true since -1 is returned. Hmmmmm... > What is going on? What am I doing wrong? Why is this happening? I am baffled! > Any help would be appreciated. > Thanks > Mike > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Try adding --vanilla when calling R, e.g. R --vanilla --no-save --args 5 < test1.R because I think you're picking up and previously stored session. You'll most likely will find that "res <- conditional1(x)" will give an error saying 'conditional1' is not defined; you need to define the function before using it. My $.02 /H On Mon, Feb 13, 2012 at 1:59 PM, Schmidt, Michael <MSchmidt at med.miami.edu> wrote:> Hi, > I am new to R. I was trying to get a very simple program to run. Take one number from the command line. If the number < 0 return -1. If number > 0 return 1 and if the number == 0 return 0. The code is in a file called test1.R > > > The code: > > #useage: R --no-save --args 5 < test1.R > args = (commandArgs(TRUE)) > x = as.numeric(args[1]) > print(x) > > res <- conditional1(x) > cat("result= ",res,"\n") > > conditional1 <- function(x){ > ? ? ? ?result <- 0 > ? ? ? ?if (x > 0) { > ? ? ? ? ? ? ? ?result <- 1 > ? ? ? ?} else if (x < 0) { > ? ? ? ? ? ? ? ?result <- -1 > ? ? ? ?} > ? ? ? ?return(result) > } > > > The output: >>R --no-save --slave --args 1 < test1.R > [1] 1 > result= ?1 >>R --no-save --slave --args -1 < test1.R > [1] -1 > result= ?-1 >>] R --no-save --slave --args 0 < test1.R > [1] 0 > result= ?-1 > > > The problem: > For arguments 1 and -1 it works as intended. For 0 (zero) it does not. If the 0 value is passed into the function named "conditional1" ?I would expect both if-statements to evaluate to false and 0 being return. From what I can tell (0 < 0) evaluates to true since -1 is returned. Hmmmmm... > What is going on? What am I doing wrong? Why is this happening? I am baffled! > Any help would be appreciated. > Thanks > Mike > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
On Tue, Feb 14, 2012 at 6:41 AM, Schmidt, Michael <MSchmidt at med.miami.edu> wrote:> So the function must come BEFORE the call to the function...I see.Yes. May be different than what you're used to but in R think of functions as just another set of "objects". Therefore they must be declared in the environment from which they are called beforehand, just like the arguments. Cheers Elai.> Thanks for that info. > Take care > Mike > > -----Original Message----- > From: ilaik9 at gmail.com [mailto:ilaik9 at gmail.com] On Behalf Of ilai > Sent: Monday, February 13, 2012 6:19 PM > To: Schmidt, Michael > Subject: Re: [R] If (x > 0) > > After placing res <- conditional1(x) ; cat("result= ",res,"\n") AFTER defining the function conditional1 (or R wouldn't know what "conditional1" is), your script worked flawlessly for me. > From the terminal in xubuntu10.04: > $ R --no-save --slave --args 1 < test1.R [1] 1 result= ?1 $ R --no-save --slave --args -1e-6 < test1.R [1] -1e-06 result= ?-1 $ R --no-save --slave --args 0 < test1.R [1] 0 result= ?0 > > So maybe you had an old (bad) version of conditional1 which was used by test1 ? > > Cheers > > > > On Mon, Feb 13, 2012 at 2:59 PM, Schmidt, Michael <MSchmidt at med.miami.edu> wrote: >> Hi, >> I am new to R. I was trying to get a very simple program to run. Take >> one number from the command line. If the number < 0 return -1. If >> number > 0 return 1 and if the number == 0 return 0. The code is in a >> file called test1.R >> >> >> The code: >> >> #useage: R --no-save --args 5 < test1.R args = (commandArgs(TRUE)) x >> as.numeric(args[1]) >> print(x) >> >> res <- conditional1(x) >> cat("result= ",res,"\n") >> >> conditional1 <- function(x){ >> ? ? ? ?result <- 0 >> ? ? ? ?if (x > 0) { >> ? ? ? ? ? ? ? ?result <- 1 >> ? ? ? ?} else if (x < 0) { >> ? ? ? ? ? ? ? ?result <- -1 >> ? ? ? ?} >> ? ? ? ?return(result) >> } >> >> ? ? ? ? ? ? ? ?result <- 1 >> ? ? ? ?} else if (x < 0) { >> ? ? ? ? ? ? ? ?result <- -1 >> ? ? ? ?} >> ? ? ? ?return(result) >> } >> >> >> The output: >>>R --no-save --slave --args 1 < test1.R >> [1] 1 >> result= ?1 >>>R --no-save --slave --args -1 < test1.R >> [1] -1 >> result= ?-1 >>>] R --no-save --slave --args 0 < test1.R >> [1] 0 >> result= ?-1 >> >> >> The problem: >> For arguments 1 and -1 it works as intended. For 0 (zero) it does not. If the 0 value is passed into the function named "conditional1" ?I would expect both if-statements to evaluate to false and 0 being return. From what I can tell (0 < 0) evaluates to true since -1 is returned. Hmmmmm... >> What is going on? What am I doing wrong? Why is this happening? I am baffled! >> Any help would be appreciated. >> Thanks >> Mike >> >> >> >> >> ? ? ? ?[[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at 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.