# TODO: Add comment
# 
# Author: E.Korsching 3.11.2011
###############################################################################



sumup.blocks <- function(x, y, condensing="mean"){
	# sum up a block of column/row data in one row where the block is identified by a label column
	# x: data.frame, y: vector containing labels
	# condensing: mean, median, sum
	# order: according to labels
	
	#ini
	nrx <- nrow(x)
	ncx <- ncol(x)
	nry <- length(y)
	nry.unique <- unique(y)[order(unique(y))]
	nry.unique.len <- length(nry.unique)
	
	#check
	if(nrx!=nry){ cat("\n Length of x and y are not allowed to differ \n"); break }
	
	#
	erg <- data.frame(matrix(0,nry.unique.len,ncx), row.names=nry.unique)
	names(erg) <- names(x)
	
	f1 <- function(x, na.rm, condensing){
		if(condensing=="mean"){ erg <- mean(x=x, ,na.rm=na.rm) }
		if(condensing=="median"){ erg <- median(x=x, na.rm=na.rm) }
		if(condensing=="sum"){ erg <- sum(x=x, na.rm=na.rm) }
		return(round(erg, digits=2))
	}
	
	for(i in 1:nry.unique.len){
		#select block of rows and condense
		erg[i,] <- apply(x[y==nry.unique[i], ], 2, f1, na.rm=FALSE, condensing=condensing)
	}
	
	#
	return(erg)
}




