Skip to content

if value is in vector, perform this function

4 messages · Louise Stevenson, Berend Hasselman, Patrick Burns

#
I think the command you want is:

if(t %in% feed_days) C_A <- 1.5 else C_A <- 0

Do not confuse `%in%` (which is essentially
"are the left-hand values in the right-hand
vector)

with

"in" of the `for` loop.

By the way,

if(t == TRUE)

is redundant -- better is:

if(t)


Pat
On 02/03/2013 23:57, Louise Stevenson wrote:

  
    
#
On 03-03-2013, at 00:57, Louise Stevenson <louise.stevenson at lifesci.ucsb.edu> wrote:

            
You have been given a correction for expression for (t %in% feed_days).

But even with that correction things will not do as you seem to want.

The argument "t" of function Daphnia is the integration time the ode solver is passing and almost certainly is NOT an element of the vector t defined at the start of your script. That "t" is the "the time sequence for which output is wanted" (see ode help); it is what is put into the output of ode.
There is no reason to assume that the Daphnia argument t is  an element of feed_days. You can easily check this by inserting a print(t) in Daphnia. So C_A will be 0 most of the time.

It would certainly help if you named the elements of the init vector and the return list of Daphnia.
In Daphnia x[2] is C_D. But what is x[1] (C_A?)?

I think you will have to look at deSolve events but I'm not sure if that is possible or required/desired with your model.

Berend
#
I forgot to say:

Also do not depend on equality in this situation.
You want to test equality with a tolerance.

See Circle 1 of 'The R Inferno':
http://www.burns-stat.com/documents/books/the-r-inferno/

I also see that 't' is a vector unlike what I was
thinking before, thus you want to use 'ifelse':

C_A <- ifelse(t %in% feed_days, 1.5, 0)

except that still leaves out the tolerance.  If
you are always only going to go by half-days, then
the following should work:

C_A <- ifelse( round(2*t) %in% round(2 * feed_days), 1.5, 0)

Pat
On 02/03/2013 23:57, Louise Stevenson wrote: