annotate histogram.r @ 12:9479e62342fa draft

Uploaded
author guerler
date Fri, 18 Apr 2014 16:40:58 -0400
parents 61a1d67f70d4
children e676c441d388
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
a889861139bc Uploaded
guerler
parents: 7
diff changeset
1 # wrapper
0
8fefbbf372be Uploaded
guerler
parents:
diff changeset
2 wrapper <- function(table, columns, options) {
8fefbbf372be Uploaded
guerler
parents:
diff changeset
3
8fefbbf372be Uploaded
guerler
parents:
diff changeset
4 # initialize output list
8fefbbf372be Uploaded
guerler
parents:
diff changeset
5 l <- list()
8fefbbf372be Uploaded
guerler
parents:
diff changeset
6
8fefbbf372be Uploaded
guerler
parents:
diff changeset
7 # loop through all columns
7
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
8 m <- list()
0
8fefbbf372be Uploaded
guerler
parents:
diff changeset
9 for (key in names(columns)) {
8fefbbf372be Uploaded
guerler
parents:
diff changeset
10 # load column data
8fefbbf372be Uploaded
guerler
parents:
diff changeset
11 column <- as.numeric(columns[key])
8fefbbf372be Uploaded
guerler
parents:
diff changeset
12 column_data <- sapply( table[column], as.numeric )
8fefbbf372be Uploaded
guerler
parents:
diff changeset
13
7
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
14 # collect vectors in list
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
15 m <- append(m, list(column_data))
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
16 }
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
17
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
18 # get min/max boundaries
12
9479e62342fa Uploaded
guerler
parents: 11
diff changeset
19 min_value <- min(unlist(m))
7
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
20 max_value <- max(unlist(m))
8
a889861139bc Uploaded
guerler
parents: 7
diff changeset
21
9
656efffe650e Uploaded
guerler
parents: 8
diff changeset
22 # check if single bin is enough
656efffe650e Uploaded
guerler
parents: 8
diff changeset
23 if (min_value == max_value) {
656efffe650e Uploaded
guerler
parents: 8
diff changeset
24 l <- append(l, max_value)
10
86068f6de925 Uploaded
guerler
parents: 9
diff changeset
25 for (key in seq(m)) {
86068f6de925 Uploaded
guerler
parents: 9
diff changeset
26 l <- append(l, 1.0)
86068f6de925 Uploaded
guerler
parents: 9
diff changeset
27 }
9
656efffe650e Uploaded
guerler
parents: 8
diff changeset
28 return (l)
656efffe650e Uploaded
guerler
parents: 8
diff changeset
29 }
656efffe650e Uploaded
guerler
parents: 8
diff changeset
30
8
a889861139bc Uploaded
guerler
parents: 7
diff changeset
31 # identify increment
12
9479e62342fa Uploaded
guerler
parents: 11
diff changeset
32 increment <- (max_value - min_value) / 10
8
a889861139bc Uploaded
guerler
parents: 7
diff changeset
33
7
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
34 # fix range and bins
8
a889861139bc Uploaded
guerler
parents: 7
diff changeset
35 bin_seq = seq(min_value, max_value, by=increment)
9
656efffe650e Uploaded
guerler
parents: 8
diff changeset
36
7
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
37 # add as first column
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
38 l <- append(l, list(bin_seq[2: length(bin_seq)]))
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
39
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
40 # loop through all columns
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
41 for (key in seq(m)) {
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
42 # load column data
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
43 column_data <- m[[key]]
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
44
0
8fefbbf372be Uploaded
guerler
parents:
diff changeset
45 # create hist data
7
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
46 hist_data <- hist(column_data, breaks=bin_seq, plot=FALSE)
0
8fefbbf372be Uploaded
guerler
parents:
diff changeset
47
8fefbbf372be Uploaded
guerler
parents:
diff changeset
48 # normalize densities
5
cbdd329ab623 Uploaded
guerler
parents: 0
diff changeset
49 count_sum <- sum(hist_data$counts)
cbdd329ab623 Uploaded
guerler
parents: 0
diff changeset
50 if (count_sum > 0) {
7
2e2d92b2ae38 Uploaded
guerler
parents: 6
diff changeset
51 hist_data$counts = hist_data$counts / count_sum
5
cbdd329ab623 Uploaded
guerler
parents: 0
diff changeset
52 }
0
8fefbbf372be Uploaded
guerler
parents:
diff changeset
53
8fefbbf372be Uploaded
guerler
parents:
diff changeset
54 # collect vectors in list
8fefbbf372be Uploaded
guerler
parents:
diff changeset
55 l <- append(l, list(hist_data$counts))
8fefbbf372be Uploaded
guerler
parents:
diff changeset
56 }
8fefbbf372be Uploaded
guerler
parents:
diff changeset
57
8fefbbf372be Uploaded
guerler
parents:
diff changeset
58
8fefbbf372be Uploaded
guerler
parents:
diff changeset
59 # return
8fefbbf372be Uploaded
guerler
parents:
diff changeset
60 return (l)
8fefbbf372be Uploaded
guerler
parents:
diff changeset
61 }