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
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
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
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
4 matches
Mail list logo