0

I'm hoping someone can help me with some simple code for condensing a vector.

I am currently working on an ODE disease model. I have a vector which represents the number of infected horses. The horses must go through "x" number of stages before they leave the compartment. The model is set up on a grid system, so for each simulation I have "n" number of cells which are represented in the vector. So the vector length is x*n.

I'd like to sum up the number of horses in all the stages for each cell, so the outcome result is a vector of length "n". So for example...

n <- 5
x <- 3

inf <- c(1,2,3,
         4,5,6,
         7,8,9,
         10,11,12,
         13,14,15)

outcome <- c(6,
             15,
             24,
             33,
             42)

Ideally, I would love not to have to put this in some sort of loop, as my code is already very computationally heavy, and I can't afford to add more time to the running of it.

zx8754
  • 46,390
  • 10
  • 104
  • 180
JDK
  • 1

1 Answers1

6

How about this:

colSums(matrix(inf,nrow=x,ncol=n))
LocoGris
  • 4,286
  • 3
  • 14
  • 30