mardi 28 mai 2019

How to store a variable for each iteration of a loop?

Currently, I'm trying to store my latitude matrix for each iteration of a loop in my code.

Currently, I'm trying to store the variable as follows:

 latitude[p] <- lat

with latitude being the variable I'm trying to store in, p being the loop number, and lat being the randomly generated latitude matrix.

The following is my reproducible code:

 p=0
 latitude = 0
 while(p<5) 
   {

   p = p + 1

   lat <- round(rnorm(n=50, m = 55, sd = 8),0)
   lon <- round(rnorm(n=50, m = 76, sd = 20),0)
   no2 <- round(rnorm(n=100, m = .3, sd = .1),2)

   partial_data <- data.frame(lat,lon,no2)
   coordinates(partial_data) = ~lat+lon
   spplot(partial_data,'no2')

   latitude[p] <- lat
 }

Currently, I'm getting the error:

 In latitude[p] <- lat :
   number of items to replace is not a multiple of replacement length

and when I call "latitude[1]" I get a single number as output. I would like a full random matrix generated for the loop as output.

Thank you!




Aucun commentaire:

Enregistrer un commentaire