# TODO: Add comment
# 
# Author: E.Korsching Sep 10, 2014
###############################################################################



rm.rows.cols <- function(x, flag="all", rmc=NA, rc=1, verbose=F){
	# remove rows or columns from a matrix or data.frame
	# flag: all: rows containing solely 'rmc' values will be deleted
	# flag: any: rows containing one or more 'rmc' values will be deleted
	# rmc: number or "character" or constant e.g. NA to be recognized
	# rc: remove rows: 1, remove columns: 2
	
	if(is.na(rmc)){
		y <- is.na(x)
	}else{
		y <- x==rmc
	}
	
	#
	if(flag=="all"){
		pos1 <- apply(y,rc,all)
	}
	if(flag=="any"){
		pos1 <- apply(y,rc,any)
	}
	
	if(verbose){
		cat("\n number of ",if(rc==1){ "rows" }else{ "cols" }," with ",flag," '",rmc,"' : ",sum(pos1)," of ",if(rc==1){ nrow(x) }else{ ncol(x) },"\n")
		posN <- apply(y,rc,sum)
		print(posN)
		cat("\n")
	}
	
	if(rc==1){ return( x[!pos1,,drop=F] ) }else{ return( x[,!pos1,drop=F] ) }
}



#aa <- matrix(1:9,3,3)
#rm.rows.cols(x=aa, flag="any", rmc=3, rc=2, verbose=T)
#aa <- matrix(letters[c(1:3,3,3,4,5:7)],3,3)		#LETTERS
#rm.rows.cols(x=aa, flag="any", rmc="c", rc=1, verbose=T)
#rm.rows.cols(x=aa, flag="any", rmc="c", rc=2, verbose=T)



