我不知道您得到了什么错误,因为我可以运行您的函数而不出现任何错误。您可以通过组合具有相同赋值的逻辑测试来简化代码:
x1 <- c('0/3' , '1/3' , '2/3' , '3/3' , './.' , '0/0' , '0/1' , '1/2' , '1/1' , '2/2' , '0/2')
replacement<-function(x){
x=replace(x,which(x=='0/3'),0)
x=replace(x,which(x=='1/3'),1)
x=replace(x,which(x=='2/3'),1)
x=replace(x,which(x=='3/3'),2)
x=replace(x,which(x=='./.'),0)
x=replace(x,which(x=='0/0'),0)
x=replace(x,which(x=='0/1'),1)
x=replace(x,which(x=='1/2'),1)
x=replace(x,which(x=='1/1'),2)
x=replace(x,which(x=='2/2'),2)
x=replace(x,which(x=='0/2'),0)
x
}
replacement_2<-function(x){
x[x %in% c('0/3', './.', '0/0', '0/2')] <- 0
x[x %in% c('1/3', '2/3', '0/1', '1/2')] <- 1
x[x %in% c('3/3', '1/1', '2/2' )] <- 2
x
}
replacement(x1)
# [1] "0" "1" "1" "2" "0" "0" "1" "1" "2" "2" "0"
replacement_2(x1)
# [1] "0" "1" "1" "2" "0" "0" "1" "1" "2" "2" "0"