计算2个向量之间的断点距离

时间:2018-03-29 01:38:09

标签: r vector permutation bioinformatics

我有2个向量;

a<-c(1,2,4,3,5,6)   
b<-c(1,4,3,2,5,6) 

a和b之间的断点数为3 ([1,2] [2,4] [3,5])

b和a之间的断点数为3 ([1,4] [2,3] [2,5])

我基本上想检查数字是否相邻并打印出来,所以我不必每次都手工完成

1 个答案:

答案 0 :(得分:2)

这是两个解决方案。首先让

a <- c(1, 2, 4, 3, 5, 6)
b <- c(1, 4, 3, 2, 6, 5)

注意我切换了b的最后两个元素。然后

# Creating a vector of pairs of subsequent elements of x
fun <- function(x) paste(head(x, -1), tail(x, -1))
BP <- function(x, y) fun(a)[!fun(a) %in% fun(b)]
BP(a, b)
# [1] "1 2" "2 4" "3 5" "5 6"

考虑此订单。也就是说,它包含5 6作为断点。或者,如果订单无关紧要,而且只是彼此相邻:

BP <- function(x, y) fun(a)[!(fun(a) %in% fun(b) | fun(a) %in% fun(rev(b)))]
BP(a, b)
# [1] "1 2" "2 4" "3 5"

然后length(BP(a, b))给出距离。

相关问题