applying self-defined function over list (for-loops?)


# missing * operator added before tan
dl <- function(lat_radians,solar_declination){
  dl <- (24/pi)*(acos((-tan(lat_radians))*tan(solar_declination)))
#> Warning in acos((-tan(lat_radians)) * tan(solar_declination)): NaNs produced

See the FAQ: How todo a minimal reproducible example reprex for beginners. It helps better frame the question, as well as expose potential problems such as this (unless NaN is agreeable).

Every Rproblem can be though of with advantage as the interaction of three objects— an existing object, x , a desired object,y, and a function, f, that will return a value of y given x as an argument. In other words, school algebra— f(x) = y. Any of the objects can be composites.

Applied to this problem x is a composite object of two vectors lat_radians and solar_declination, the proto-y_0 is a lat_radians by solar_declination matrix object of dim [4000,365], one part of f is dl and the other part is a function to apply populate each element of y_0 by applying dl to each element to produce y.

Here's a toy example

my_func <- function(x,y) x*y

mat <- matrix(nrow = 3, ncol = 3, byrow = TRUE)

for (i in 1:3) {
  for (j in 1:3) mat[i,j] = my_func(i,j)

#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    2    4    6
#> [3,]    3    6    9