percent

percent, function

def nosort process percent(a: number): number

Returns the share of each value a within its group, i.e. a / sum(a).

Examples

table T = with
  [| as A |]
  [| 1 |]
  [| 2 |]
  [| 3 |]

T.P = percent(T.A)

show table "Percent of total" with
  T.A
  T.P

This outputs the following table:

A P
1 0.1666667
2 0.3333333
3 0.5
table U = with
  [| as A, as G |]
  [| 1, "a" |]
  [| 2, "a" |]
  [| 3, "b" |]

U.P = percent(U.A) by U.G

show table "Percent by group" with
  U.G
  U.A
  U.P

This outputs the following table:

G A P
a 1 0.3333333
a 2 0.6666667
b 3 1
User Contributed Notes
0 notes + add a note