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-tp26041165p26041165.html Sent from the R help mailing list archive at Nabble.com.
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.
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 251I 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 > >