John Mount explains, using Conway’s Game of Life, the importance of using vectors in R over scalars:
R is an interpreted programming language with vectorized data structures. This means a single R command can ask for very many arithmetic operations to be performed. This also means R computation can be fast. We will show an example of this using Conway’s Game of Life.
Conway’s Game of Life is one of the most interesting examples of cellular automata. It is traditionally simulated on a rectangular grid (like a chessboard) and each cell is considered either live or dead. The rules of evolution are simple: the next life grid is computed as follows:
-
To compute the state of a cell on the next grid sum the number of live cells in the eight neighboring cells on the current grid.
-
If this sum is 3 or if the current cell is live and the sum is 2 or 3, then the cell in the next grid will be live.
Not only is the R code faster, but it’s also terser.