view histogram.r @ 11:61a1d67f70d4 draft

Uploaded
author guerler
date Fri, 18 Apr 2014 15:11:06 -0400
parents 86068f6de925
children 9479e62342fa
line wrap: on
line source

# utilities
roundUp <- function(x) 10 * ceiling(x/10)
roundDown <- function(x) 10 * floor(x/10)

# wrapper
wrapper <- function(table, columns, options) {

    # initialize output list
    l <- list()

    # loop through all columns
    m <- list()
    for (key in names(columns)) {
        # load column data
        column <- as.numeric(columns[key])
        column_data <- sapply( table[column], as.numeric )

        # collect vectors in list
        m <- append(m, list(column_data))
    }
    
    # get min/max boundaries
    max_value <- max(unlist(m))
    min_value <- min(unlist(m))
    
    # round number to base 10
    min_value <- roundDown(min_value)
    max_value <- roundUp(max_value)
    
    # check if single bin is enough
    if (min_value == max_value) {
        l <- append(l, max_value)
        for (key in seq(m)) {
            l <- append(l, 1.0)
        }
        return (l)
    }
    
    # identify increment
    increment <- roundUp((max_value - min_value) / 10)
    
    # fix range and bins
    bin_seq = seq(min_value, max_value, by=increment)
 
    # add as first column
    l <- append(l, list(bin_seq[2: length(bin_seq)]))
    
    # loop through all columns
    for (key in seq(m)) {
        # load column data
        column_data <- m[[key]]
        
        # create hist data
        hist_data <- hist(column_data, breaks=bin_seq, plot=FALSE)
        
        # normalize densities
        count_sum <- sum(hist_data$counts)
        if (count_sum > 0) {
            hist_data$counts = hist_data$counts / count_sum
        }

        # collect vectors in list
        l <- append(l, list(hist_data$counts))
    }
    

    # return
    return (l)
}