8
|
1 # utilities
|
|
2 roundUp <- function(x) 10^ceiling(log10(x))
|
|
3 roundDown <- function(x) 10^floor(log10(x))
|
|
4
|
|
5 # wrapper
|
0
|
6 wrapper <- function(table, columns, options) {
|
|
7
|
|
8 # initialize output list
|
|
9 l <- list()
|
|
10
|
|
11 # loop through all columns
|
7
|
12 m <- list()
|
0
|
13 for (key in names(columns)) {
|
|
14 # load column data
|
|
15 column <- as.numeric(columns[key])
|
|
16 column_data <- sapply( table[column], as.numeric )
|
|
17
|
7
|
18 # collect vectors in list
|
|
19 m <- append(m, list(column_data))
|
|
20 }
|
|
21
|
|
22 # get min/max boundaries
|
|
23 max_value <- max(unlist(m))
|
|
24 min_value <- min(unlist(m))
|
|
25
|
8
|
26 # round number to base 10
|
9
|
27 min_value <- roundDown(min_value)
|
8
|
28 max_value <- roundUp(max_value)
|
|
29
|
9
|
30 # check if single bin is enough
|
|
31 if (min_value == max_value) {
|
|
32 l <- append(l, max_value)
|
|
33 l <- append(l, 1.0)
|
|
34 return (l)
|
|
35 }
|
|
36
|
8
|
37 # identify increment
|
9
|
38 increment <- roundUp((max_value - min_value) / 10)
|
8
|
39
|
7
|
40 # fix range and bins
|
8
|
41 bin_seq = seq(min_value, max_value, by=increment)
|
9
|
42
|
7
|
43 # add as first column
|
|
44 l <- append(l, list(bin_seq[2: length(bin_seq)]))
|
|
45
|
|
46 # loop through all columns
|
|
47 for (key in seq(m)) {
|
|
48 # load column data
|
|
49 column_data <- m[[key]]
|
|
50
|
0
|
51 # create hist data
|
7
|
52 hist_data <- hist(column_data, breaks=bin_seq, plot=FALSE)
|
0
|
53
|
|
54 # normalize densities
|
5
|
55 count_sum <- sum(hist_data$counts)
|
|
56 if (count_sum > 0) {
|
7
|
57 hist_data$counts = hist_data$counts / count_sum
|
5
|
58 }
|
0
|
59
|
|
60 # collect vectors in list
|
|
61 l <- append(l, list(hist_data$counts))
|
|
62 }
|
|
63
|
|
64
|
|
65 # return
|
|
66 return (l)
|
|
67 }
|