I want to calculate growing degree days with several bases, using minimum daily temp, and maximum daily temp. I would like to do this without a for loop if possible to minimize my code.
bases <- c(40,45,50)
tmin <- runif(10,30,70)
tmax <- runif(10,55,95)
I want to find the number of growing degree days for each base for each of the ten days I have fake temperature data for. There should be 10 output values for each of the 3 bases.
I've tried mapply as follows:
gdd_func <- function(tmin,tmax,bases){
(tmin + tmax)/2 - bases}
test <- mapply(gdd_func,tmin,tmax,bases)
This produces an incorrect output where I honestly don't know what it's doing. I want the output to be equal to running the above function 3 different times with the different bases. Each output would have 10 gdd values corresponding to the differing bases. How would I do this using an apply function of some kind? Or do I need something more?
I think you should encapsulate tmin
and tmax
within a list
, e.g.,
> mapply(gdd_func, list(tmin), list(tmax), bases)
[,1] [,2] [,3]
[1,] 19.119648 14.119648 9.119648
[2,] 6.119249 1.119249 -3.880751
[3,] 19.523350 14.523350 9.523350
[4,] 13.023240 8.023240 3.023240
[5,] 31.083015 26.083015 21.083015
[6,] 32.281061 27.281061 22.281061
[7,] 18.062584 13.062584 8.062584
[8,] 18.047198 13.047198 8.047198
[9,] 21.518514 16.518514 11.518514
[10,] 28.440563 23.440563 18.440563