title: Creating animation (.gif) in R using .png files
body: <p>Sample data</p>
  
  <pre><code> location &lt;- c("A","B","C")
   years &lt;- c(2001,2002,2003)
  
   for(l in seq_along(location)){
      for(y in seq_along(years)){
         loc &lt;- location[l]
         yr &lt;- years[y]
         png(paste0(loc,".",yr,".png"))
         plot(rnorm(10))
         dev.off()
     }
     }
  </code></pre>
  
  <p>For each location X year combination, I generated png files. My goal is to combine for each location, all the years in a single gif.file to show as animation.</p>
  
  <p>I am doing this</p>
  
  <pre><code>  library(magick)
  
    # convert each png file as magick object 
    for(l in seq_along(location)){
      for(y in seq_along(years)){
         loc &lt;- location[l]
         yr &lt;- years[y]
  
         png.dat &lt;- image_read(paste0(loc,".",yr,".png"))
  
         assign(paste0(loc,".",yr),png.dat)
     }}
  </code></pre>
  
  <p>This gives me following files for location A, B and C:</p>
  
  <p>A.2001, A.2002, A.2003
  B.2001, B.2002, B.2003
  C.2001, C.2002, C.2003</p>
  
  <pre><code>  # stack the objects for one location, and create animation
    A.c &lt;- c(A.2001,A.2002,A.2003)
    A.img &lt;- image_scale(A.c)
    A.ani &lt;- image_animate(A.img, fps = 1, dispose = "previous")
    image_write(A.ani, paste0("A_animation.gif"))
  
    # repeat for B and C
     B.c &lt;- c(B.2001,B.2002,B.2003)
     B.img &lt;- image_scale(B.c)
     B.ani &lt;- image_animate(B.img, fps = 1, dispose = "previous")
     image_write(B.ani, paste0("B_animation.gif"))
  
   # stack the objects for one location, and create animation
    C.c &lt;- c(C.2001,C.2002,C.2003)
    C.img &lt;- image_scale(C.c)
    C.ani &lt;- image_animate(C.img, fps = 1, dispose = "previous")
    image_write(C.ani, paste0("C_animation.gif"))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Jyzma.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jyzma.gif" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/fNZch.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fNZch.gif" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/7Ybqi.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7Ybqi.gif" alt="enter image description here"></a></p>
  
  <p>My issue is that in reality I have over 100 locations and 30 years. So the above steps on creating animation becomes manual. Does anyone have a quicker method to do the above task.</p>