[R] ifelse

Uwe Ligges
Sat, 24 Oct 2009 18:28:56 -0000

pking wrote:
> When I run this code from an R-script:
>        ddd = 360 + round ( atan2(-u,-v) / d2r )
>        print(class(ddd))
>        print(ddd)
>        ifelse ( ddd>360, ddd-360, ddd )
>        print(ddd)
>
> I get this output:
> [1] "numeric"
> [1] 461 213 238 249 251
> [1] 461 213 238 249 251
>
> Why does ifelse not change the 461 to 101?


Because you never changed ddd

You want the last but one line to be

ddd <- ifelse ( ddd>360, ddd-360, ddd )

I guess.



> I recreated the vector ddd and ran the same ifelse
> code and it did work as expected.
>
>> ddd <- c(461, 213, 238, 249, 251)
>> print(class(ddd))
> [1] "numeric"
>
>> print(ddd)
> [1] 461 213 238 249 251
>
>> ifelse ( ddd>360, ddd-360, ddd )
>> print(ddd)
> [1] 101 213 238 249 251


I doubt.


> What am I missing?

That you made an assignment

ddd <- ifelse ( ddd>360, ddd-360, ddd )

that you have not told us (nor youself?) in your second case.

Best,
Uwe Ligges


> Patrick King
>
>