[R] ifelse

HBaize
Sat, 24 Oct 2009 17:41:55 -0000

What I think you are missing is that you didn't change ddd. The ifelse
statement does not assign values to the ddd object. To change ddd it would
read:

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

So when you enter "print(ddd)" you get the content of the original
object,
which has not changed.

What is missing from your examples below is the output of the ifelse
statement. When I run your second example (I can't run the first because you
didn't supply the data objects)
I get the following:

> 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 )
[1] 101 213 238 249 251
>  print(ddd)
[1] 461 213 238 249 251
>

Which is perfectly reasonable given that the ifelse does not change the ddd
object.



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?
>
> 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
>
> What am I missing?
>
> Patrick King
>
>
>

--
View this message in context:
http://www.nabble.com/ifelse-tp26041165p26041312.html
Sent from the R help mailing list archive at Nabble.com.