根据@Cath 的初始评论,将字符向量转换为因子似乎可以通过将文本值映射到可以在函数中使用的整数来提供一种解决方法。编辑:请注意,重新排序因子水平会改变最终结果。我对不和谐函数的了解还不够,无法说出这是否是预期的行为。
# Original Character vectors
arg1 <- c("b","c","a","d")
arg2 <- c("b","c","d","a")
# Translate character vectors into factors
all_levels <- unique(arg1, arg2)
arg1 <- factor(arg1, levels = all_levels)
arg1
[1] b c a d
Levels: b c a d
arg2 <- factor(arg2, levels = all_levels)
arg2
[1] b c d a
Levels: b c a d
# This maps each text string to a number
as.numeric(arg1)
[1] 1 2 3 4
as.numeric(arg2)
[1] 1 2 4 3
# Use the underlying numeric data in the function
require(asbio)
sum(ConDis.matrix(as.numeric(arg1), as.numeric(arg2))==-1,na.rm=TRUE)
[1] 1
编辑:对因子水平进行排序会改变最终输出
arg1 <- c("b","c","a","d")
arg2 <- c("b","c","d","a")
all_levels <- sort(unique(arg1, arg2)) # sorted
arg1 <- factor(arg1, levels = all_levels)
arg2 <- factor(arg2, levels = all_levels)
sum(ConDis.matrix(as.numeric(arg1), as.numeric(arg2))==-1,na.rm=TRUE)
[1] 5