8
|
1 # utilities
|
11
|
2 roundUp <- function(x) 10 * ceiling(x/10)
|
|
3 roundDown <- function(x) 10 * floor(x/10)
|
8
|
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)
|
10
|
33 for (key in seq(m)) {
|
|
34 l <- append(l, 1.0)
|
|
35 }
|
9
|
36 return (l)
|
|
37 }
|
|
38
|
8
|
39 # identify increment
|
9
|
40 increment <- roundUp((max_value - min_value) / 10)
|
8
|
41
|
7
|
42 # fix range and bins
|
8
|
43 bin_seq = seq(min_value, max_value, by=increment)
|
9
|
44
|
7
|
45 # add as first column
|
|
46 l <- append(l, list(bin_seq[2: length(bin_seq)]))
|
|
47
|
|
48 # loop through all columns
|
|
49 for (key in seq(m)) {
|
|
50 # load column data
|
|
51 column_data <- m[[key]]
|
|
52
|
0
|
53 # create hist data
|
7
|
54 hist_data <- hist(column_data, breaks=bin_seq, plot=FALSE)
|
0
|
55
|
|
56 # normalize densities
|
5
|
57 count_sum <- sum(hist_data$counts)
|
|
58 if (count_sum > 0) {
|
7
|
59 hist_data$counts = hist_data$counts / count_sum
|
5
|
60 }
|
0
|
61
|
|
62 # collect vectors in list
|
|
63 l <- append(l, list(hist_data$counts))
|
|
64 }
|
|
65
|
|
66
|
|
67 # return
|
|
68 return (l)
|
|
69 }
|