I am using R for agent-based historical simulation and the code works but slowly. It loops through timesteps updating a dataframe of attributes of agents, and another with summary of overall state after each timestep (a generation). Looping above that are a few runs of each different parameter setting. Though it begins with 100 agents, under extreme settings (high S, low A) after e.g. five generations the population can grow above a thousand. I read that updating a matrix is faster than dataframe so I converted summary to a matrix. But I also hear that vectorisation is best so before I change agents to matrix I wonder if anyone please could suggest a way to make it more vectorised? Here is the code:I am using R for agent-based historical simulat