# Writing a function containing a while-loop to estimate incubation duration and total energy spent during development

I have the a function named EDP (see below) that will be used to estimate energy and percent development. For every hour, I want to record the temperature, percent development, and energy used. Each iteration of the loop will total up the amount of time (hours) and energy required for development. I need to input Tave and A (Trackers) with an expected output vector containing Tave, A, Energy(J), and total time to hatch (in days). With a Tave = 28 and A = 5, the output should come out as : 28, 5, 1941.0049, and 53.375. My main question is how do I even structure a loop like this?

This is what I have for the EDP function I created earlier (It contains the A and Tave values:

``````EDP <- function(t, Tave, A) {
Soil <- SST(t, Tave, A)
B <- Energy(Soil)
C <- DevRate(Soil)
return(c(B, C))
}
``````

This is what I've tried to no avail:

``````IncenerDev <- vector("double")
Day <- (t * 24)
Tavetracker <- Tave + 1
Atracker <- A + 1
while(t <= 5) {
EDP(t) <- Tavetracker
}
``````

I am not sure of what you are trying to do here. You are assigning an object to the result of the function.

I would create a function of t that gives you your result (is `EDP(t)` the result you want ? why assigning to it ?)
Also do you want to increment t at each step ? If so you'll need to do it in the while loop (using t <- t +1) or using a for loop.

There may be some useful reading if you are not familiar yet

Hope it helps

With EDP(t) <- Tavetracker, I was trying to create a counter but as I have been practicing, I just realized I shouldn't do make one of that nature. What I have done so far is modified the code so that I am creating a new function based off the old 'EDP' function and then giving it a vector output. That is followed by the counters (trackers) for t, PercDev, and A (1/2 daily temperature: used in another created function). I then state the while statement and it's conditions and what happens to the counters if those conditions are met. An error message states that argument "A" is missing, with no default. I apologize if this is not making sense. I am new to creating loops, especially nesting loops within functions. This is the code below:

``````Inc_Energy_Dev <- function(Tave, A) {
Inc_Energy_Dev <- vector("double")
t <- 0
PercDev <- 1
A <- 0
while(t <= (3600/24) | PercDev <= 1.0)
Inc_Energy_Dev <- EDP(Tave, A)
if(EDP <= 0) {
t <- 0 | PercDev <- 0
} else {
t <- t + 1
A <- A + 0.5
PercDev <- PercDev + 1
}
}

Inc_Energy_Dev(Tave = 28, A = 5)
``````

From the given problem what I can interpret is that you want a vector output for t ranging from 1 to 5.
A doubt, will Tave and A changes in each iteration or remain constant?

``````EDP <- function(t, Tave, A) {
Soil <- SST(t, Tave, A) # don't know about these functions
B <- Energy(Soil)       ##
C <- DevRate(Soil)      ##
D <- paste(Tave,Soil,B,C) # Single value which will be returned as character
return(D)
}

IncenerDev <- vector()

for (t in 1:5) {

result = vector()  # Sorry, it should be result not a

Day <- (t * 24)
Tavetracker <- Tave + 1
Tave <- Tave + 1 # iterating Tave
Atracker <- A + 1
A <- A + 1  # iterating A also
result <- EDP(t, Tave, ATracker)

IncenerDev <- rbind(IncenerDev,a)

}

``````

Output format

`````` "2, 5, 5J, 0.05%"   "3, 8, 8J, 0.08%"   "4, 11, 11J, 0.11%" "5, 14, 14J, 0.14%"
 "6, 17, 17J, 0.17%"

#Just added "J" in B and  "%" in C
``````

is this helpful? 1 Like

Your code was helpful in understanding what some of my outputs should be. I need to structure this as a while-loop that stops when development is 100% (i.e. PercDevtracker = 1.0 or when t = 150 days. Through my understanding, A and Tave will change across each iteration of the loop. This is what I have modified from the original code I posted with the question. I get an error code stating argument 'A' is missing.

``````Inc_Energy_Dev <- function(Tave, A) {
Inc_Energy_Dev <- vector("double")
Day <- (t * 24)
ttracker <- 0
PercDevtracker <- 0
while(ttracker <= 150 | PercDevtracker <= 1.0)
Inc_Energy_Dev <- EDP(Tave, A)
if(EDP <= 0) {
t <- 0 | PercDev <- 0
} else {
t <- t + 1
PercDevtracker <- PercDevtracker + 1
}
}

Inc_Energy_Dev(Tave = 28, A = 5)
``````

The error is shown because, EDP function takes 3 inputs/arguments, but you are providing only 2 on the given line of code

``````while(t <= (3600/24) | PercDev <= 1.0)
Inc_Energy_Dev <- EDP(Tave, A)   # 2 arguments/inputs only. EDP(t=Tave,Tave=A, A=????{missing})
``````

whereas,

``````EDP <- function(t, Tave, A) {...}  # 3 arguments/inputs needed
``````

I put in all three arguments in this manner:

``````Inc_Energy_Dev <- function(t, Tave, A) {
Inc_Energy_Dev <- vector("double")
t <- 0
PercDev <- 1
EnergyExp <- 0
while(t <= (3600/24) | PercDev <= 1.0)
Inc_Energy_Dev <- EDP(t <- t + 1)
if(EDP <= 0) {
t <- 0 | PercDev <- 0
} else {
t <- t + 1
PercDev <- PercDev + 1
}
result <- EDP(t, Tave, A)
}

Inc_Energy_Dev(Tave = 28, A = 5)
Inc_Energy_Dev(Tave = 30, A = 10)
``````

However, it's saying that argument 'A' is missing with no default.

With intuition I tried to update your code I don't know if it will work correct, I only tried to rectify the syntax errors. I may have upset the functional requirement you needed. Please, look

``````Inc_Energy_Dev <- function(Tave, A) {
#Initially had 3 inputs removed 't' because you are creating t variable in below line of code.
#Inc_Energy_Dev <- vector("double") # Guess don't need
result<- vector("double")
t <- 0
PercDev <- 1
EnergyExp <- 0
while(t <= (3600/24)){
# PercDev initially had value 1 so while loop while exit
Inc_Energy_Dev <- EDP(t,Tave,A) # updated input to EDP function

if(Inc_Energy_Dev > 0) {
# updated 'if' condition as EDP is a function it does not store value like variables.
# but, Inc_Energy_Dev is a variable of type double, it will store value returned by EDP

result <- rbind(result,Inc_Energy_Dev)
# result will be a vector contains all values of Inc_Energy_Dev variable at all values of t

t <- t + 1

#iterating after result

}# Assuming from your code you don't want to consider negative values, hence only positive values will be stored in result with if condition.
} # added while block without this only first line below while will be in loop.
return(result)
}

result1 <- Inc_Energy_Dev(Tave = 28, A = 5)
result2 <- Inc_Energy_Dev(Tave = 30, A = 10)

``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.