mercredi 20 avril 2016

if-statement recursive function in R

Looking to run a simple function within a function (recursive / embedded).

As you can see, an ifelse statement is used, based on a lagged outcome of the function.

r <- t(t(runif(40, -2, 2)))

test1 <- function(x)
{
  a <- 1
  b <- ifelse(x < a, 1,
             ifelse(lag(test1, 1) == 1,
                    ifelse(x < 0.5, 1, 0), 0))

  b
}

output <- test1(r[,1])

I have tried to come up with other solutions or with the function reduce, but cannot get it to work.

Aucun commentaire:

Enregistrer un commentaire