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



#### split columns containing different levels into one level only columns

split2uni <- function(x, z, marker=1, prefix="", rx=F){
	# split columns containing different levels into one level only columns
	# x: column, is character or will be converted to be character, transformation is based on vector positions
	# z: vector of identifier/levels which should be considered for splitting
	#  not given identifiers which are part of the x vector result in a 0 character value
	# marker: the number value denoting the event,   prefix: "" or string - if one z element prefix=column name
	# rx:T: regex pattern: e.g. "[1]{1}[a,b,c]{1}" given for each (!) of the z element(s)
	# return a list of numeric vector(s) having a 1 where the level identifier is at vector position
	#  and 0 where the level identifier is not at vector position
	if(!is.character(x)){
		cat("\nx vector converted to character and check the result")
		x <- as.character(x)
	}
	if(!is.character(z)){
		cat("\nz vector converted to character and check the result")
		z <- as.character(z)
	}
	
	xlen <- length(x)
	zlen <- length(z)
	mb <- matrix(0,xlen,zlen)
	if(zlen==1){
		dimnames(mb)[[2]] <- prefix
	}else{
		dimnames(mb)[[2]] <- paste(prefix, z, sep="")
	}
	if(rx){
		for(i in 1:zlen){
			logi <- grepl(pattern=z[i],x,perl=T)
			mb[logi, i] <- marker
		}
	}else{
		for(i in 1:zlen){
			mb[x==z[i], i] <- marker
		}
	}
	return(mb)
}


#a <- split2uni(c("a","1","a","2","a","2","2","1"), z=c("a","1","2"), marker=2, prefix="a")
#a <- split2uni(c("a","1","a","2","a","2","2","1"), z=c("a","2"))

#a <- split2uni(c("a","1c","a","2","a","2","2","1a"), z=c("a","[1]{1}[a,b,c]{1}","2"), marker=1, prefix="x_", rx=T)

#a <- split2uni(c(1,2,2,2,3,3,1,3), z=c(1,3,2))
#a <- split2uni(c(1,2,2,2,3,3,1,3), z=c(1,2))
#a <- split2uni(c(1,2,2,2,3,3,1,3), z=c("1","3","2"))
#a <- split2uni(c("1","2","2","2","3","3","1","3"), z=c(1,3,2))



#### split a column containing a range of numbers into sub-range only columns

split2subrange <- function(x, z, start, end, marker=1, prefix=""){
	# split columns containing a continous range of numbers into one subranges denoted by numbers
	#  categorization
	# x: column, is numeric
	# z: break points which should be considered for splitting
	# start,end: assumed range of values
	#  resulting identifiers which are part of the x vector result in a 0 character value
	# marker: the number value denoting the event
	# return a list of numeric vector(s) having a 1 where the level identifier is at vector position
	#  and 0 where the level identifier is not at vector position
	if(!is.numeric(x)){
		cat("\nx vector is not numeric")
	}
	if(!is.numeric(z)|!is.numeric(start)|!is.numeric(end)){
		cat("\nz vector / start / end not numeric")
	}
	xlen <- length(x)
	zlen <- length(z)
	zl1 <- zlen+1
	mat <- matrix(0,zl1,2)
	mat[,1] <- c(start,z)
	mat[,2] <- c(z,end)
	mb <- matrix(0,xlen,zl1)
	namevec <- vector("character",zl1)
	for(i in 1:zl1){
		namevec[i] <- paste(mat[i,],collapse="_")
	}
	dimnames(mb)[[2]] <- paste(prefix, namevec, sep="")
	
	for(i in 1:zl1){
		if(i<zl1){
			mb[(x>=mat[i,1] & x<mat[i,2]), i] <- marker
		}else{
			mb[(x>=mat[i,1] & x<=mat[i,2]), i] <- marker
		}
	}
	return(mb)
}


#a <- split2subrange(c(1,2,7,3,5,2,12,9,1,6,11,3), z=c(3,8,12), start=0, end=13, marker=2, prefix="a")
#a$`0_3` ; a[["0_3"]]



