[R] Problem with if

Patrick Burns pburns at pburns.seanet.com
Sun Nov 11 10:40:21 CET 2012


This is Circle 8.1.30 of 'The R Inferno'.

http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

The example even uses 3.

On 10/11/2012 16:58, David Winsemius wrote:
>
> On Nov 10, 2012, at 8:34 AM, Haszun wrote:
>
>> Why it always gives me a 3?
>>
>>> fun=function(x) {
>> + if (x<-3) {
>
> The above code assigns 3 to x.
>
>
>> + return(x)
>> + } else {
>> + if(x<2) {
>> + return(x^2-1)
>> + } else {
>> + return(log(x))
>> + }}}
>>>
>>> fun(-5)
>> [1] 3
>>> fun(0)
>> [1] 3
>>> fun(10)
>> [1] 3
>>> fun(-10)
>> [1] 3
>>>
>>
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Problem-with-if-tp4649180.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> Alameda, CA, USA
>
> ______________________________________________
> 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.
>

-- 
Patrick Burns
pburns at pburns.seanet.com
twitter: @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')




More information about the R-help mailing list