hammingDist

Odległość Hamming'a:

hammingDist<-function(x,y){
	if(length(x)!=1 | length(y)!=1)
		stop("x,y - not a single string!")
	if(nchar(x)!=nchar(y))
		stop("x,y - not equal length!")
	tmpX<-strsplit(as.character(x),'')[[1]]
	tmpY<-strsplit(as.character(y),'')[[1]]
	res<-NULL
	for(i in 1:length(tmpX)){
		res[i]<-tmpX[i]!=tmpY[i]
	}
	return(sum(res))
}

Created by Pretty R at inside-R.org

hammingDist<-function(x,y){
	if(length(x)!=1 | length(y)!=1)
		stop("x,y - not a single string!")
	if(nchar(x)!=nchar(y))
		stop("x,y - not equal length!")
	tmpX<-strsplit(as.character(x),'')[[1]]
	tmpY<-strsplit(as.character(y),'')[[1]]
	res<-NULL
	for(i in 1:length(tmpX)){
		res[i]<-tmpX[i]!=tmpY[i]
	}
	result<-list()
	result[['hammingDist']]<-sum(res)
	tmpX[res]<-"."
	result[['commonPattern']]<-paste(tmpX,sep="",collapse="")
	return(result)
}

Created by Pretty R at inside-R.org

hammingDist<-function(x,y){
	if(length(x)!=1 | length(y)!=1)
		stop("x,y - not a single string!")
	if(nchar(x)!=nchar(y))
		stop(paste("x,y - not equal length!\n","x= ", nchar(x),"\ny= ",nchar(y),sep=""))
	tmpX<-strsplit(as.character(x),'')[[1]]
	tmpY<-strsplit(as.character(y),'')[[1]]
	res<-NULL
	for(i in 1:length(tmpX)){
		res[i]<-tmpX[i]!=tmpY[i]
	}
	result<-list()
	result[['hammingDist']]<-sum(res)
	tmpX[res]<-"."
	result[['commonPattern']]<-paste(tmpX,sep="",collapse="")
	return(result)
}

Created by Pretty R at inside-R.org

admin wtorek, 08 maj 2012 - 11:58 am | | Blog-R