簡體   English   中英

比較不同長度的多個向量

[英]compare multiple vectors of different lengths

我想擴展什么比較不同長度的多個向量,計算相同的元素,並打印出那些相同和不同的元素 我想編寫一個循環,以便我可以對十個不同的向量進行成對比較,以找出每個比較的共同點,對於所有可能的成對選項。 下面偽代碼的主要比較部分是有效的,從上一篇文章開始,但這只是為了比較 A 和 B,我想比較 A 與 C、A 與 #D、B 與 C 等。 ..

vectors to be compared: A, B, C, D, E, F, G, H, I, J
set global variable for first vector to be compared
set global variable for second vector to be compared

#vetors -- these are subsets of my real vectors, which are more like 50 - 200 elements long
A <- c("866_78", "1137_78", "1721_78", "1745_79", "1910_79", "1972_76", 
        "2776_77",  "3049_79", "3084_15",  "3995_78", "4066_33", "4431_15")
B <- c("866_78", "1137_78", "1721_78", "1745_79", "1910_79", "1972_76", 
       "2776_77",  
       "3049_79", "3084_15",  "3995_78")
C <- c("866_78", "1137_78", "1910_79", "1972_76", "2776_77",  
       "3049_79", "3084_14",  "3995_78", "4066_36", "4431_19", "4885_78")
D <- c("866_78", "1137_78", "1721_78", "1745_79", "1910_79", "1972_76", 
      "2773_77",  
       "3049_79", "3084_12",  "3995_78", "4066_36", "4431_19", "4885_78")
E <- c("866_78", "1137_78", "1721_78", "1745_79", "1910_79", "1972_76", 
      "2776_77",  
       "3049_79", "3084_17", "4431_19", "4885_78")
F <- c("868_78", "1137_78", "1721_78", "1745_79", "1910_79", "1972_76", 
       "2776_77",  
       "3049_79", "3084_15",  "3995_78", "4066_36", "4431_19", "4885_78")
G <- c("866_78", "1837_78", "1721_78", "1972_76", "2776_77",  
       "3049_79", "3084_15",  "3995_78", "4066_36", "4431_19", "4885_78")
H <- c("866_78", "1137_78", "1721_78", "1745_79", "1910_79", "1972_76", 
        "2776_77",  
       "3049_79", "3084_15",  "3995_78", "4066_36", "4431_19", "4885_78")
I <- c("866_78", "1137_28", "1721_78", "1745_79", "1910_79", "1972_76", 
       "2776_77",  
       "3995_78", "4066_36", "4431_19", "4885_78")
J <- c("866_78", "1137_78", "1721_78", "1745_79", "1910_79", "1972_76", 
       "2776_77",  
       "3049_79", "3084_18",  "3995_78", "4066_36", "4431_19", "4885_78")

for(i ???)
{
compare.SNPs <- function(A, B) {
  # consider only unique names
  A.u <- unique(A)
  B.u <- unique(B)
  common.A.B <- intersect(A.u, B.u)
  diff.A.B <- setdiff(A.u, B.u)
  diff.B.A <- setdiff(B.u, A.u)
  uncommon.A.B <- union(diff.A.B, diff.B.A)
  cat(paste0("The sets have ", length(common.A.B), " SNPs in common:"))
  print(common.A.B)
  print(paste0("The sets have ", length(uncommon.A.B), " SNPs not in 
  common:"))
  print(paste0("In the first set, but not in the second set:"))
  print(diff.A.B)
  print(paste0("Not in the first set, but in the second set:"))
  print(diff.B.A)

}
compare.SNPs(A,B)
}

任何示例代碼的指導將不勝感激。

真誠的,艾拉

xx<-combn(LETTERS[1:10],2)
for (i in 1:dim(xx)[2]) {
      cat(paste0("Comparing ", xx[1,i], " and ", xx[2,i],": "))
      compare.SNPs(get(xx[1,i]),get(xx[2,i]))
    }

(此外,沒有理由將function()調用置於 for 循環內)。

首先,你應該告訴你的函數compare.SNPs給你一些東西......好吧......作為return而不僅僅是打印。 這是通過return(list(common.AB, diff.AB, diff.BA))

compare.SNPs <- function(A, B) {
  # consider only unique names
  A.u <- unique(A)
  B.u <- unique(B)
  common.A.B <- intersect(A.u, B.u)
  diff.A.B <- setdiff(A.u, B.u)
  diff.B.A <- setdiff(B.u, A.u)
  uncommon.A.B <- union(diff.A.B, diff.B.A)
  cat(paste0("The sets have ", length(common.A.B), " SNPs in common:"))
  print(common.A.B)
  print(paste0("The sets have ", length(uncommon.A.B), " SNPs not in 
               common:"))
  print(paste0("In the first set, but not in the second set:"))
  print(diff.A.B)
  print(paste0("Not in the first set, but in the second set:"))
  print(diff.B.A)
  return(list(common.A.B, diff.A.B, diff.B.A))
}

所以現在這個函數將return一個包含 3 個元素的list :兩個向量的公共元素,然后是 2 組差異。

mylist <- list(A, B, C, D, E, F, G, H, I, J)
allcomparisons <- list()
for(i in 1:length(mylist))
{
  for(j in 1:length(mylist)) {
    allcomparisons <- c(allcomparisons, compare.SNPs(mylist[[i]], mylist[[j]]))
  }
}

比較 A 和 J 是比較 10x10 個元素,總共會給你 300 個元素:首先是 A 和 A 的公共元素,然后是 A 和 A 之間的差異,然后是 A 和 A 之間的差異,然后是 A 和 A 的公共元素乙等。 然后,您可以像訪問任何其他列表一樣訪問allcomparisons的元素。 例如,您可以檢查all(allcomparisons[[4]]==compare.SNPs(A,B)[[1]])

[1] TRUE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM