annotate histogramdiscrete.r @ 35:f294f5c9608c draft

Uploaded
author guerler
date Fri, 09 May 2014 00:58:55 -0400
parents f92f68399023
children bfd966f30073
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
34
f92f68399023 Uploaded
guerler
parents:
diff changeset
1 # wrapper
f92f68399023 Uploaded
guerler
parents:
diff changeset
2 wrapper <- function(table, columns, options) {
f92f68399023 Uploaded
guerler
parents:
diff changeset
3
f92f68399023 Uploaded
guerler
parents:
diff changeset
4 # initialize output list
f92f68399023 Uploaded
guerler
parents:
diff changeset
5 l <- list()
f92f68399023 Uploaded
guerler
parents:
diff changeset
6
f92f68399023 Uploaded
guerler
parents:
diff changeset
7 # loop through all columns
f92f68399023 Uploaded
guerler
parents:
diff changeset
8 m <- list()
f92f68399023 Uploaded
guerler
parents:
diff changeset
9 for (key in names(columns)) {
f92f68399023 Uploaded
guerler
parents:
diff changeset
10 # load column data
f92f68399023 Uploaded
guerler
parents:
diff changeset
11 column <- as.numeric(columns[key])
f92f68399023 Uploaded
guerler
parents:
diff changeset
12
f92f68399023 Uploaded
guerler
parents:
diff changeset
13 # ensure string column
f92f68399023 Uploaded
guerler
parents:
diff changeset
14 column_data <- sapply( table[column], as.character )
f92f68399023 Uploaded
guerler
parents:
diff changeset
15
f92f68399023 Uploaded
guerler
parents:
diff changeset
16 # collect vectors in list
f92f68399023 Uploaded
guerler
parents:
diff changeset
17 m <- append(m, list(column_data))
f92f68399023 Uploaded
guerler
parents:
diff changeset
18 }
f92f68399023 Uploaded
guerler
parents:
diff changeset
19
f92f68399023 Uploaded
guerler
parents:
diff changeset
20 # get alphabetically sorted bins
f92f68399023 Uploaded
guerler
parents:
diff changeset
21 bins <- sort(unique(unlist(m)))
f92f68399023 Uploaded
guerler
parents:
diff changeset
22
f92f68399023 Uploaded
guerler
parents:
diff changeset
23 # add first column
f92f68399023 Uploaded
guerler
parents:
diff changeset
24 l <- append(l, list(bins))
f92f68399023 Uploaded
guerler
parents:
diff changeset
25
f92f68399023 Uploaded
guerler
parents:
diff changeset
26 # loop through all columns
f92f68399023 Uploaded
guerler
parents:
diff changeset
27 for (key in seq(m)) {
f92f68399023 Uploaded
guerler
parents:
diff changeset
28 # reset bins
f92f68399023 Uploaded
guerler
parents:
diff changeset
29 bins = sapply(bins, function(v) { 0 })
f92f68399023 Uploaded
guerler
parents:
diff changeset
30
f92f68399023 Uploaded
guerler
parents:
diff changeset
31 # load column data
f92f68399023 Uploaded
guerler
parents:
diff changeset
32 column_data <- m[[key]]
f92f68399023 Uploaded
guerler
parents:
diff changeset
33
f92f68399023 Uploaded
guerler
parents:
diff changeset
34 # create hist data
f92f68399023 Uploaded
guerler
parents:
diff changeset
35 table_data <- table(column_data)
f92f68399023 Uploaded
guerler
parents:
diff changeset
36
f92f68399023 Uploaded
guerler
parents:
diff changeset
37 # transfer counts to bins
f92f68399023 Uploaded
guerler
parents:
diff changeset
38 for (id in names(table_data)) {
f92f68399023 Uploaded
guerler
parents:
diff changeset
39 bins[id] <- table_data[id]
f92f68399023 Uploaded
guerler
parents:
diff changeset
40 }
f92f68399023 Uploaded
guerler
parents:
diff changeset
41
f92f68399023 Uploaded
guerler
parents:
diff changeset
42 # normalize densities
f92f68399023 Uploaded
guerler
parents:
diff changeset
43 total <- length(column_data)
f92f68399023 Uploaded
guerler
parents:
diff changeset
44 if (total > 0) {
f92f68399023 Uploaded
guerler
parents:
diff changeset
45 bins = bins / total
f92f68399023 Uploaded
guerler
parents:
diff changeset
46 }
f92f68399023 Uploaded
guerler
parents:
diff changeset
47
f92f68399023 Uploaded
guerler
parents:
diff changeset
48 # collect vectors in list
f92f68399023 Uploaded
guerler
parents:
diff changeset
49 l <- append(l, list(bins))
f92f68399023 Uploaded
guerler
parents:
diff changeset
50 }
f92f68399023 Uploaded
guerler
parents:
diff changeset
51
f92f68399023 Uploaded
guerler
parents:
diff changeset
52 # return
f92f68399023 Uploaded
guerler
parents:
diff changeset
53 return (l)
f92f68399023 Uploaded
guerler
parents:
diff changeset
54 }