README.md

Neighbour Apply

Tobias Krause, 2018

Find the entire project description in nhapply.pdf. Only german.

The Idea behind Neighbour Apply

A common task when building cellular automata, pixel-simulations or similar is to apply a function on the neighbourhood of a pixel. It can be tireding, implementing such functions, so that they are efficient and flexible.

The packages' main function nhapply processes pixel-neighbourhoods significantly faster than for-loops and is as easy to use as sapply. At the same time it maintains great flexibility in defining neighbourhoods and custom functions.

How to use nhapply

nhapply works like the apply family; input is a matrix, FUN is the function to apply on the neighbourhood of each pixel.

Possible Modes of Input for FUN

optimised functions are: "mean", "sum", "max", "min", "which.max", "which.min", "maxcount", "maxcountvalue"; check documentation for more details

Modification of Neighbourhoods

Example: Conways Game of Life

game_field <- matrix(sample(0:1, 100 * 100, rep = TRUE, prob = c(7, 3)), nrow = 100)

i <- 1
while (i < 10){
  # apply function to neighbours
  sum_neighbours <- nhapply(game_field, sum)

  # game-variation:
  # sum_neighbours <- nhapply(game_field, sum,
  # neighb_type = "diamond", width = 2)

  # process result
  game_field[sum_neighbours < 2 || sum_neighbours > 3] <- 0
  game_field[sum_neighbours == 3] <- 1

  # plotting
  par(pty = "s")
  plot(col(game_field), rev(row(game_field)), col = game_field,
       pch = 15, yaxt="n", xaxt="n", ylab="", xlab="",
       main = paste("Conways Game of life frame", i))
  i <- i+1
}


Tobiaspk/RPackage_nhapply documentation built on Feb. 19, 2021, 12:13 a.m.