-2

i want to use a function for each row in a vector.

when I try my code I receive this message: the condition has length > 1 and only the first element will be used

function(Inn, x, y, z, xx, b, i, Dp){

  if (Inn == 0) {
    if (runif(1) <= x) { # success.probability.imitation 
      absorptive.capacity <- max(y, z) 
    }
    else{
      absorptive.capacity <- min(x, x + b/sqrt(i) - Dp)
    } 
  } else{
    if (runif(1) <= xx) { # sucess innovation
      absorptive.capacity <- y + b / sqrt(i)
    } else{
      absorptive.capacity <- y + b / sqrt(i) - Dp
    }

  }
  if (absorptive.capacity > 1) {
    absorptive.capacity = 1
  }
  return(absorptive.capacity) 
}

absorptive.capacity(c(0, 1), c(0.5, 0), c(0.7, 0.8), 0.7, c(0, 06), 0.5, c(64, 94), 0.06)

I want return each value after that transformation, this is the purpose for my function. Some one can help me?

  • It isn't clear what you are trying to do, but typing `?ifelse` at the console might give you some hints. `ifelse` is vectorized. `if ... else` isn't. – John Coleman Aug 21 '19 at 01:20
  • Thanks, You give me a good hint! But how could I resolve the last one `if` condition for each row?? – Felipe Silva Aug 21 '19 at 01:31
  • When you run the function with the set above, you have two numbers for each variable. So when running the function, you get to the line ```if (Inn == 0) ```, it can only check this for one variable. If you want to run the function for variables of length > 1, that might be possible to figure out. – markhogue Aug 21 '19 at 01:37
  • Thanks guys! I solved the problem! – Felipe Silva Aug 21 '19 at 01:48

1 Answers1

-1
absorptive.capacity<-function(Inn, x, y, z, xx, b, i, Dp){

  ifelse( Inn == 0, 
          ifelse(runif(1) <= x,
                           absorptive.capacity <- max(y, z),
                           absorptive.capacity <- min(x, x + b/sqrt(i) - Dp)),
          ifelse(runif(1) <= xx,
                 absorptive.capacity <- y + b / sqrt(i),
                 absorptive.capacity <- y + b / sqrt(i) - Dp))
  ifelse(absorptive.capacity > 1,
         absorptive.capacity == 1,
         absorptive.capacity == absorptive.capacity)

  return(absorptive.capacity) 
}