Dear Friends.
I found a puzzling phenomenon in R when you use 'if' within a function:
# defining a function aaa
aaa=function(a)
{if (a==1) {aaa=1};
if (a!=1) {aaa=2}
}
# using the function:> b=20
> bbb=aaa(b)
> bbb
[1] 2> typeof(bbb)
[1] "double">
>
> c=1
> ccc=aaa(c)
> ccc
NULL> typeof(ccc)
[1] "NULL"
It seems that only the last 'if' phrase works. Is it an instrinsic
weakness
of R? Is there a way to get around it? ( I use 'elseif' to get around
this
when there are only two cases to choose from, but what if there are more
than two cases to choose from?)
Best
Yuchen
[[alternative HTML version deleted]]
Hi Yuchen, In R, if you do not put an explicit return statement in the function, the value the function returns is the value of the last statement in the function. Unlike VB, it does not matter whether you assign this value to aaa (which is identical to your function name) or b or c or x etc. So either use an explicit return statement or make sure that the last statement in the function produces the right result. --- Yuchen Luo <realityrandom at gmail.com> wrote:> Dear Friends. > I found a puzzling phenomenon in R when you use 'if' > within a function: > > # defining a function aaa > aaa=function(a) > {if (a==1) {aaa=1}; > if (a!=1) {aaa=2} > } > > # using the function: > > b=20 > > bbb=aaa(b) > > bbb > [1] 2 > > typeof(bbb) > [1] "double" > > > > > > c=1 > > ccc=aaa(c) > > ccc > NULL > > typeof(ccc) > [1] "NULL" > > It seems that only the last 'if' phrase works. Is it > an instrinsic weakness > of R? Is there a way to get around it? ( I use > 'elseif' to get around this > when there are only two cases to choose from, but > what if there are more > than two cases to choose from?) > > Best > Yuchen > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
_______________________________________________________________________________________
R doesn't use the 'functionname = result' idiom to return a value
from a
function. It looks like you're after:
aaa <- function(a)
{
if(a == 1) return(1)
if(a != 1) return(2)
}
or
aaa <- function(a)
{
if(a == 1) 1
else 2
}
see ?return
--
Hong Ooi
Senior Research Analyst, IAG Limited
388 George St, Sydney NSW 2000
+61 (2) 9292 1566
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Yuchen Luo
Sent: Thursday, 21 June 2007 3:28 PM
To: r-help at stat.math.ethz.ch
Subject: [R] "if" within a function
Dear Friends.
I found a puzzling phenomenon in R when you use 'if' within a function:
# defining a function aaa
aaa=function(a)
{if (a==1) {aaa=1};
if (a!=1) {aaa=2}
}
# using the function:> b=20
> bbb=aaa(b)
> bbb
[1] 2> typeof(bbb)
[1] "double">
>
> c=1
> ccc=aaa(c)
> ccc
NULL> typeof(ccc)
[1] "NULL"
It seems that only the last 'if' phrase works. Is it an instrinsic
weakness
of R? Is there a way to get around it? ( I use 'elseif' to get around
this
when there are only two cases to choose from, but what if there are more
than two cases to choose from?)
Best
Yuchen
[[alternative HTML version deleted]]
______________________________________________
R-help at stat.math.ethz.ch 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.
_______________________________________________________________________________________
The information transmitted in this message and its attachme...{{dropped}}
> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Yuchen Luo > Sent: Wednesday, June 20, 2007 10:28 PM > To: r-help at stat.math.ethz.ch > Subject: [R] "if" within a function > > Dear Friends. > I found a puzzling phenomenon in R when you use 'if' within a > function: > > # defining a function aaa > aaa=function(a) > {if (a==1) {aaa=1}; > if (a!=1) {aaa=2} > } > > # using the function: > > b=20 > > bbb=aaa(b) > > bbb > [1] 2 > > typeof(bbb) > [1] "double" > > > > > > c=1 > > ccc=aaa(c) > > ccc > NULL > > typeof(ccc) > [1] "NULL" > > It seems that only the last 'if' phrase works. Is it an > instrinsic weakness > of R? Is there a way to get around it? ( I use 'elseif' to > get around this > when there are only two cases to choose from, but what if > there are more > than two cases to choose from?) > > Best > Yuchen >Yuchen, In R, a function returns the last value evaluated. In your case, if the argument passed to aaa() is equal to 1, the value returned is the value of the last if statement which is null. You can tell aaa() to return the value you want with something like this aaa<-function(a) {if (a==1) return(1) if (a!=1) return(2) } Hope this is helpful, Dan Daniel J. Nordlund Research and Data Analysis Washington State Department of Social and Health Services Olympia, WA 98504-5204
You did not specify what your function should return and thus it returns
the last value by default. If a!=1, the value returned is 2, however if
a==1, the function tries to return the result of {if (a!=1) {aaa=2}}.
You can correct this easily by modifying your function like this:
aaa=function(a)
{if (a==1) {aaa=1}
if (a!=1) {aaa=2}
aaa
}
Petr
Yuchen Luo napsal(a):> Dear Friends.
> I found a puzzling phenomenon in R when you use 'if' within a
function:
>
> # defining a function aaa
> aaa=function(a)
> {if (a==1) {aaa=1};
> if (a!=1) {aaa=2}
> }
>
> # using the function:
>> b=20
>> bbb=aaa(b)
>> bbb
> [1] 2
>> typeof(bbb)
> [1] "double"
>>
>> c=1
>> ccc=aaa(c)
>> ccc
> NULL
>> typeof(ccc)
> [1] "NULL"
>
> It seems that only the last 'if' phrase works. Is it an instrinsic
weakness
> of R? Is there a way to get around it? ( I use 'elseif' to get
around this
> when there are only two cases to choose from, but what if there are more
> than two cases to choose from?)
>
> Best
> Yuchen
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
--
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic