comparison histogram.r @ 7:2e2d92b2ae38 draft

Uploaded
author guerler
date Thu, 17 Apr 2014 17:35:24 -0400
parents 6a11aeb8bd39
children a889861139bc
comparison
equal deleted inserted replaced
6:6a11aeb8bd39 7:2e2d92b2ae38
2 2
3 # initialize output list 3 # initialize output list
4 l <- list() 4 l <- list()
5 5
6 # loop through all columns 6 # loop through all columns
7 m <- list()
7 for (key in names(columns)) { 8 for (key in names(columns)) {
8 # load column data 9 # load column data
9 column <- as.numeric(columns[key]) 10 column <- as.numeric(columns[key])
10 column_data <- sapply( table[column], as.numeric ) 11 column_data <- sapply( table[column], as.numeric )
11 12
13 # collect vectors in list
14 m <- append(m, list(column_data))
15 }
16
17 # get min/max boundaries
18 max_value <- max(unlist(m))
19 min_value <- min(unlist(m))
20
21 # fix range and bins
22 bin_seq = seq(min_value, max_value, by=10)
23
24 # add as first column
25 l <- append(l, list(bin_seq[2: length(bin_seq)]))
26
27 # loop through all columns
28 for (key in seq(m)) {
29 # load column data
30 column_data <- m[[key]]
31
12 # create hist data 32 # create hist data
13 hist_data <- hist(column_data, plot=FALSE) 33 hist_data <- hist(column_data, breaks=bin_seq, plot=FALSE)
14 34
15 # normalize densities 35 # normalize densities
16 count_sum <- sum(hist_data$counts) 36 count_sum <- sum(hist_data$counts)
17 if (count_sum > 0) { 37 if (count_sum > 0) {
18 hist_data$counts=hist_data$counts/count_sum 38 hist_data$counts = hist_data$counts / count_sum
19 } 39 }
20 40
21 # collect vectors in list 41 # collect vectors in list
22 l <- append(l, list(hist_data$breaks[2: length(hist_data$breaks)]))
23 l <- append(l, list(hist_data$counts)) 42 l <- append(l, list(hist_data$counts))
24 } 43 }
25 44
26 # make sure length is fine
27 n <- max(sapply(l, length))
28 ll <- lapply(l, function(X) {
29 c(as.character(X), rep('2147483647', times = n - length(X)))
30 })
31 l <- do.call(cbind, ll)
32 45
33 # return 46 # return
34 return (l) 47 return (l)
35 } 48 }