Iteration - random walk

Hi!

I am trying to create an iteration (2 for-loops) based on a data frame of 2 col and 25 rows.

I have created a new date frame that countain the position of
all bacterias for each iteration (120 iterations). So I now have a data frame of 240 col (x1, y1, x2, y2, x3, y3...) and 25 rows. all 25 bacteria should move at the same time.

I am trying to create a GIF of all the changing position. This part, I think is correct :
# Création des fichiers .png
png(file="EX.png", width=200, height=200)

for (iter in 1:max_iteration){
  plot.new()
  text(.5, .5, i, cex = 6)
}
dev.off()

# Conversion des fichiers .png en un seul fichier .gif avec ImageMagick
system("convert -delay 80 *.png EX.gif")
# On supprime les fichiers .png
file.remove(list.files(pattern=".png"))

However, I cannot figure out how to arrange my 2 for-loops. I would like to produce and save as many plots as there are iterations (120).

My question is: it possible to create a for loop extracting only 2 column at a time? So for the first iteration columns 1 and 2, for iteration 2 columns 3 and 4 ... etc. ?

Here is an example of using a for loop to pull out pairs of columns from a data frame. I only made six columns and the code only prints the data rather than making a plot.

DF <- data.frame(x1 = 1:4, y1 = 5:8, x2 = 11:14, y2 = 15:18, x3 = 21:24, y3 = 25:28)
for (i in seq(1, 5, 2)) {
   tmp <- DF[ , c(i, i+1)]
   print(paste("i =", i))
   print(tmp)
 }
[1] "i = 1"
  x1 y1
1  1  5
2  2  6
3  3  7
4  4  8
[1] "i = 3"
  x2 y2
1 11 15
2 12 16
3 13 17
4 14 18
[1] "i = 5"
  x3 y3
1 21 25
2 22 26
3 23 27
4 24 28

Does that help you get started?

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.