annotate histogram.r @ 7:2e2d92b2ae38 draft

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