# Simple pattern detection in numerical data

A while ago I helped out a colleague who was testing some methods to detect economic (in)activity of companies, based on their quarterly tax declarations (our institute has access to such data). One of the thing my colleague wanted was to detect simple patterns, such as a bunch of zero-declarations, followed by positive ones, or the other way around. After fiddling around for half an hour I came up with the following solution which is so "typically R" that I decided share it, so here it goes.

In stead of directly detecting the patterns, I decided to write a distance function which counts the number of cells in a row which do not comply with some pattern. A pattern can be specified with

1 (value is positive)
-1 (value is negative)
0 (value is zero)
NA (value is missing)

A vector of values indicates a pattern. Below is the function:

`# Number of cells in a row not matching a simple pattern## x: numeric data.frame  or matrix# pattern: row pattern to detect, vector of length(ncol(x)) with# entry : meaning#  -1   : < 0#   1   : > 0#   NA  : NA #   0   : 0#patternDist <- function(x,pattern){    apply(sign(x), 1,        function(row){            v <- row - pattern            v[is.na(pattern) & is.na(row)] <- 0            v[xor(is.na(pattern), is.na(row))] <- 1            sum( v != 0 )    })}`

Created by Pretty R at inside-R.org

The type of numbers to be detected was so simple that it was sufficient to use the sign function. Also, it's kinda funny to note that the whole function consists of a single apply-statement (which is never a goal in itself of course, but nevertheless...). I think it is a nice example of the power of R. Here's an example of how to use it.

`set.seed(1)x <- matrix(rnorm(40),nrow=10)x[sample(40,4)] <- 0x[sample(40,4)] <- NAx <- as.data.frame(x)x           V1          V2          V3          V41  -0.6264538  1.51178117  0.91897737          NA2   0.1836433  0.38984324  0.78213630 -0.102787733  -0.8356286  0.00000000  0.07456498  0.387671614   1.5952808 -2.21469989 -1.98935170 -0.053805045          NA  1.12493092  0.61982575 -1.377059566  -0.8204684  0.00000000 -0.05612874 -0.414994567   0.4874291 -0.01619026 -0.15579551 -0.394289958          NA  0.00000000          NA -0.059313409   0.5757814  0.82122120 -0.47815006  1.1000253710 -0.3053884  0.59390132  0.41794156  0.76317575patternDist(x,pattern=c(-1,1,1,NA))[1] 0 2 2 4 2 3 4 4 3 1`

Created by Pretty R at inside-R.org

And indeed, only the first row completely complies with the pattern negative-positive-positive-missing.

This entry was posted in Uncategorized. Bookmark the permalink.