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]]

______________________________________________
[email protected] 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.

Reply via email to