Learn R Programming

segregation (version 1.1.0)

entropy: Calculates the entropy of a distribution

Description

Returns the entropy of the distribution defined by group.

Usage

entropy(data, group, weight = NULL, base = exp(1))

Value

A single number, the entropy.

Arguments

data

A data frame.

group

A categorical variable or a vector of variables contained in data.

weight

Numeric. (Default NULL)

base

Base of the logarithm that is used in the entropy calculation. Defaults to the natural logarithm.

Examples

Run this code
d <- data.frame(cat = c("A", "B"), n = c(25, 75))
entropy(d, "cat", weight = "n") # => .56
# this is equivalent to -.25*log(.25)-.75*log(.75)

d <- data.frame(cat = c("A", "B"), n = c(50, 50))
# use base 2 for the logarithm, then entropy is maximized at 1
entropy(d, "cat", weight = "n", base = 2) # => 1

Run the code above in your browser using DataLab