Re: [R] help understanding why #function(x, y) (if((x-y)>=0) {2^(x-y)} else{-(2^abs(x-y))})# doesn't work like I think it should

2008-06-03 Thread Gabor Grothendieck
Try this: data.frame(x, y, folds = (x == y) + sign(x - y) * 2 ^ abs(x - y)) or replace data.frame with cbind if you prefer a matrix result. On Tue, Jun 3, 2008 at 9:47 PM, ALAN SMITH <[EMAIL PROTECTED]> wrote: > Hello R users and developers, > I am trying to write several functions (fairly new a

Re: [R] help understanding why #function(x, y) (if((x-y)>=0) {2^(x-y)} else{-(2^abs(x-y))})# doesn't work like I think it should

2008-06-03 Thread Erik Iverson
Alan - ALAN SMITH wrote: Hello R users and developers, I am trying to write several functions (fairly new at this) in order to avoid using loops on large data frames (because they are slow). I wrote the function below and it seems to get hung on the first part of the if statement and then appli

Re: [R] help understanding why #function(x, y) (if((x-y)>=0) {2^(x-y)} else{-(2^abs(x-y))})# doesn't work like I think it should

2008-06-03 Thread Simon Blomberg
Use ifelse() rather than if () {} else {}. It's vectorized and very useful for applying to elements along vectors. The latter idiom is much better for control of flow in programs and functions. folds <- function (x, y) ifelse(x-y >= 0, 2^(x-y), -(2^abs(x-y))) z <- folds(x, y) > z [1] 161

[R] help understanding why #function(x, y) (if((x-y)>=0) {2^(x-y)} else{-(2^abs(x-y))})# doesn't work like I think it should

2008-06-03 Thread ALAN SMITH
Hello R users and developers, I am trying to write several functions (fairly new at this) in order to avoid using loops on large data frames (because they are slow). I wrote the function below and it seems to get hung on the first part of the if statement and then applies that condition to rest of