annotate histogram.r @ 65:48c591b9e917 draft

Uploaded
author guerler
date Fri, 27 Feb 2015 02:43:07 -0500
parents 12eb81b00cd9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
1 # wrapper
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
2 wrapper <- function(table, columns, options) {
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
3
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
4 # initialize output list
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
5 l <- list()
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
6
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
7 # loop through all columns
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
8 m <- list()
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
9 for (key in names(columns)) {
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
10 # load column data
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
11 column <- as.numeric(columns[key])
50
12eb81b00cd9 Uploaded
guerler
parents: 45
diff changeset
12 column_data <- suppressWarnings(as.numeric(as.character(table[column][[1]])))
12eb81b00cd9 Uploaded
guerler
parents: 45
diff changeset
13
23
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
14 # collect vectors in list
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
15 m <- append(m, list(column_data))
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
16 }
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
17
45
b84a193361be Uploaded
guerler
parents: 36
diff changeset
18 # identify optimal breaks
b84a193361be Uploaded
guerler
parents: 36
diff changeset
19 hist_data <- hist(unlist(m), plot=FALSE)
b84a193361be Uploaded
guerler
parents: 36
diff changeset
20 breaks <- hist_data$breaks;
23
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
21
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
22 # add as first column
45
b84a193361be Uploaded
guerler
parents: 36
diff changeset
23 l <- append(l, list(breaks[2: length(breaks)]))
23
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
24
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
25 # loop through all columns
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
26 for (key in seq(m)) {
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
27 # load column data
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
28 column_data <- m[[key]]
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
29
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
30 # create hist data
45
b84a193361be Uploaded
guerler
parents: 36
diff changeset
31 hist_data <- hist(column_data, breaks=breaks, plot=FALSE)
23
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
32
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
33 # normalize densities
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
34 count_sum <- sum(hist_data$counts)
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
35 if (count_sum > 0) {
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
36 hist_data$counts = hist_data$counts / count_sum
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
37 }
45
b84a193361be Uploaded
guerler
parents: 36
diff changeset
38
23
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
39 # collect vectors in list
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
40 l <- append(l, list(hist_data$counts))
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
41 }
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
42
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
43 # return
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
44 return (l)
0e3df2630d9b Uploaded
guerler
parents:
diff changeset
45 }