last day (15 days later) » 

1:30 AM
1
Q: Not sure if the match function is used correctly or there is other mistakes in this function in r

Giuseppe PetriI am trying to apply a function to calculate some variables. This is how my output should look like: In column D, in red, is the way column C should be calculated. This is why I used the function match. The number to substract (1000) should be the one at the first R occurrence from column A. I n...

 
there was no input for seed.mass.fraction i.e. if(seed.mass.fraction < p_hi) i.e. I don't find an input for that in the arguments
 
do you mean like an initial value of zero?
 
I meant if(seed.mass.fraction < p_hi) seed.mass.fraction is not defined or is not coming as input argument
 
I see. The problem I have, due to my limited skills, is that I don't see a way to put a cap to seed.mass.fraction in the function. It cannot be higher that p_hi.
 
What i meant is that you can pass seed.mass.fraction as another input argument to the function. When you do if() the object is not found inside the function. You are assuming that it should find from the dataset, but that can happen only if you add as an input
Try the solution below. I couldn't test it as your data showed is in image
I guess you have only two columns c_total_biomass and stage in the data, so where does seed.mass.fraction comes from. It is not previously defined anywhere before you do if(seed.mass.fraction < p_hi)
I am just trying to understand your function. You may know it better than me regarding the 'seed.mass.fraction' input value
 
1:30 AM
What I am trying to do is to indicate the function to calculate seed.mass.fraction until it reaches p_hi. Once it reaches this value seed.mass.fraction should be equal to p_hi. seed.mass.fraction is meant to be the outcome of the function.
 
But, when you reach the if condition seed.mass.fraction was not even defined. You may need to define seed.mass.fraction <- <somevalue> at the top
Probably, the if condtion should be afterwards
 
Yes!. I needed to define it first. I'll edit my question. Still, I have an error, but I am sure it is associated with the way I used match. THANKS
 
Do you think the initial value can be 0?
 
Yes, it should be 0.
 
Now, in the updated solution, it works, but the values are all 0
 
1:30 AM
Yes. Need to fix the value of c_total_biomass to the one of the first occurrence of R which is 1000.
 
When you are doing the rowwise, the value of seed.mass.fraction as 0 always meet if(seed.mass.fraction < p_hi) and thus when you multoiply rate_increase_hi * c_biomass_during_rep it is 0 as you are assigning rate_increase_hi to 0 when stage is "V" or c_biomass_during_rep' as 0
 
but if you replace c_total_biomass[match("R", stage)] with a value let's say 1000 it gives the correct values.
 
You meant this line if(stage == "V") {c_biomass_during_rep = 0} to if(stage == "V") {c_biomass_during_rep = 1000}?
But, it is multilying by the other 0, returns 0
 
No, this one: if(stage == "R") {c_biomass_during_rep = c_total_biomass - c_total_biomass[match("R", stage)]} replace by this if(stage == "R") {c_biomass_during_rep = c_total_biomass - 1000}
The problem is that it doesn't has to be 1000. It needs to be the c_total_biomass of the very first occurrence stage = "R". It is not a fixed value. Sorry for taking so much time.
 
Try the updated code
Idea is to create a column in the dataset 'first_c_total_biomass' which will be the value of c_total_biomass where stage is 'R' for the first time. Then, you do the rowwise
 

last day (15 days later) »