Search code examples
revent-simulation

While loop for creating multiple resources with capacity


I need to create 52 resources with capacity 2 in the Simmer simulation package. I am trying to do this by using a while loop that creates these resources for me, instead of creating each resource myself. The idea is that I have a while loop as given below. In each loop, a resource should be created called Transport_vehicle1, Transport_vehicle2, ..., Transport_vehicle52, with capacity 2.

Now I do not know how to insert the number i in the name of the resource that I am trying to create

i<-1
while (i<=52)
{ env %>%
  add_resource("Transport_vehicle"[i],capacity = 2)
  i <- i+1
}     

Could someone please help me out? Thanks!


Solution

  • You can use the paste method to concatenate the string and the number:

    i<-1
    while (i<=52)
    { env %>%
      add_resource(paste("Transport_vehicle", i),capacity = 2)
      i <- i+1
    } 
    

    If you do not want a space between the string and the number add the sep="" argument

    paste("Transport_vehicle", i, sep="")
    

    or use

    paste0("Transport_vehicle", i)