Home > Net >  Algorithm to send to par
Algorithm to send to par

Time:06-08

I have a large list of data that I am plotting, however my lists of data can change rather dynamically. It can range from 1 to 20 plots. Now 20 plots is pretty ugly to plot but rarely will I get any more than 10.

So essentially, here's the reasoning behind my algorithm:

For n = 1:
(1,1)
n=2
(1,2)
n=3
(2,2)
n=4
(2,2)
n=5
(2,3)
n=6
(2,3)
n=7
(3,3)
n=8
(3,3)
n=9
(3,3)
..
..

So we add an extra row and once that matches n by the number of plots, then we add 1 to the column, and when the number of plots matches n we add 1 to the row, and continue this repeatedly. So for example, when n=7 we wait until n=9 to fill maximum plots and when n=10 we should get (3,4).

I am having difficulties figuring out the algorithm for this, and would kindly appreciate some help figuring it out!

CodePudding user response:

The following function implements the algorithm as you describe it for a given number of plots n

plot_size <- function(n) {
  
  for(i in seq(n)) {
    if(i == 1) start <- c(1, 1)
    else if(start[1] == start[2] & i > prod(start)) start[2] <- start[2]   1
    else if(start[2] > start[1] & i > prod(start)) start[1] <- start[1]   1
  }
  return(start)
}

We can check it produces the correct output by printing the values from 1 to 20.

lapply(1:20, plot_size)
#> [[1]]
#> [1] 1 1
#> 
#> [[2]]
#> [1] 1 2
#> 
#> [[3]]
#> [1] 2 2
#> 
#> [[4]]
#> [1] 2 2
#> 
#> [[5]]
#> [1] 2 3
#> 
#> [[6]]
#> [1] 2 3
#> 
#> [[7]]
#> [1] 3 3
#> 
#> [[8]]
#> [1] 3 3
#> 
#> [[9]]
#> [1] 3 3
#> 
#> [[10]]
#> [1] 3 4
#> 
#> [[11]]
#> [1] 3 4
#> 
#> [[12]]
#> [1] 3 4
#> 
#> [[13]]
#> [1] 4 4
#> 
#> [[14]]
#> [1] 4 4
#> 
#> [[15]]
#> [1] 4 4
#> 
#> [[16]]
#> [1] 4 4
#> 
#> [[17]]
#> [1] 4 5
#> 
#> [[18]]
#> [1] 4 5
#> 
#> [[19]]
#> [1] 4 5
#> 
#> [[20]]
#> [1] 4 5

Created on 2022-06-07 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related