Henry
Henry

Reputation: 1686

Creating a list in R when I have another list that contains relevant indexes

An analogy of the problem I'm facing is growth phases of butterflies. Lets say there are 4 stages to go from being conceived to death. I have an array which defines the days at which the transition happens, and I have an equation which tells me the size of the larva/butterfly/whatever at each of these phases (the analogy isnt exact but its close enough)

I.e.:

days <- c(5,20,50,100)
#We have transitions on day 5, 20, 50, 100
#Using a linear size=m*day+c, we calculate the size of the creature
base_size <- c(1,10,11,20)
#This defines the c in the equation above
daily_growth_rate <- c(0.01,0.03,0.03,0.05)
#This defines the gradient, m

I also have the (redundant, admittedly) information of the relative number of days between stages:

rel_days <- c(0,15,30,50)
#This is just t_n-t_(n-1) from the days array

What I would like to do is populate an array with all the days in question (which is easily done):

output_days <- c(days[1]:days[length(days)])

But then I need to populate the size on each of these days as per the equation above

The way I envision (currently) the final code to look like, is something like this:

size <- daily_base_size+relative_day*growth_rate

But I'm not quite sure how to populate these variables.

For example, relative days would look like:

relative_day <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,1,2,3......30,1,2....50)

Upvotes: 1

Views: 30

Answers (1)

akrun
akrun

Reputation: 886938

We can use sequence to create the relative_day

relative_day <- sequence(rel_days)

and then use this in the equation after replicating the 'base_size' and 'daily_growth_rate'.

rep(base_size, rel_days)+relative_day*rep(daily_growth_rate, rel_days)

Upvotes: 1

Related Questions