So in the course of generating some fake data to answer a map question, I found myself writing the following:
# Generate some fake data
lat <- seq(-90, 90, by = 5)
lon <- seq(-180, 180, by = 10)
phi <- matrix(0, nrow = length(lat), ncol = length(lon))
i <- 1
for (l1 in lat) {
j <- 1
for (l2 in lon) {
phi[i, j] <- (sin(pi * l1 / 180) * cos(pi * l2 / 180))^2
j <- j+1
}
i <- i+1
}
phi <- 1500*phi + 4500 # scale it properly
Now obviously those two central for-loops are not as R'ish as I would like. It seems like I should be able to get an mapply
or something to do the job, but sadly that returns a list, and does not really do what I want. The other applys don't seem to do the right thing either.
What am I missing here?