entropy: Information entropy

Description Usage Arguments Examples

View source: R/entropy.R

Description

Computes the information entropy (also called Shannon entropy) of a set of discrete values, or a tabulated such set.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
entropy(x, ...)

## S3 method for class 'table'
entropy(x, base = 2, ...)

## S3 method for class 'data.frame'
entropy(x, base = 2, ...)

## S3 method for class 'matrix'
entropy(x, base = 2, ...)

## Default S3 method:
entropy(x, base = 2, ...)

Arguments

x

a vector, table, data.frame or matrix. In the case of table, data.frame and matrix each row is treated as a separate set of counts or proportions, with columns representing species, types, categories etc.

...

further arguments passed to methods

base

the log base to be used.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
entropy(c(5, 5, 4, 4, 2, 3, 5))  # default is unit bits
entropy(c(5, 5, 4, 4, 2, 3, 5), base=exp(1))  # unit nats

entropy(rep(1:4, 1:4), 4)
entropy(rep(1:4, 1), 4)

entropy(as.factor(c(1, 1, 2, 3, 4, 4)))
entropy(as.character(c(1, 1, 2, 3, 4, 4)))

mtctab <- table(mtcars$cyl, mtcars$carb)
entropy(mtctab, 6)

xx <- data.frame(bee=c(0, 0, 1, 2, 3, 2, 0, 3),
                wasp=c(1, 3, 2, 0, 1, 1, 2, 1),
                  fly=c(1, 2, 4, 2, 1, 0, 1, 0),
              beetle=c(1, 0, 0, 1, 2, 2, 0, 2),
            butterfly=c(0, 0, 0, 0, 3, 1, 0, 1))

entropy(xx) 

AkselA/R-ymse documentation built on March 21, 2020, 9:52 a.m.