compare multiple vectors of different lengths, count the elements that are the same, and print out those that are the same and different

你说的曾经没有我的故事 提交于 2020-01-06 05:50:07

问题


I have five vectors with the following format, and of varying lengths. The are vectors of single nucleotide polymorphisms (SNPs)

A <- c("2179_39","2764_47","4521_24","9056_66")
B <- c("2478_39","2734_47","4531_24","2178_39","2734_47","4521_24")

In R, I would like to: pint out which SNPs match between the different vectors count the number of SNPs that match print out which SNPs do not match count the number of SNPs that do not match

I found the following script that prints out the locations where the vectors match, but I've tried a bunch of print and length functions in it, and I can't seems to get what I really want from it. I'd also like to iterate through my five vectors (i.e., A vs B, A vs C, A vs D, A vs E, etc...) if possible.

foo <- function(A,B){
  if (!isTRUE(all.equal(A,B))){
    mismatches <- paste(which(A != B), collapse = ",")
    stop(mismatches )
  } else {
    message("Yahtzee!")
  }
}

foo(A,B)

Any advice, even just a site to look at to help me integrate the print and length functions, would be great.

Ella


回答1:


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)

The sets have 1 SNPs in common:[1] "4521_24"
[1] "The sets have 7 SNPs not in common:"
[1] "In the first set, but not in the second set:"
[1] "2179_39" "2764_47" "9056_66"
[1] "Not in the first set, but in the second set:"
[1] "2478_39" "2734_47" "4531_24" "2178_39"


来源:https://stackoverflow.com/questions/50380588/compare-multiple-vectors-of-different-lengths-count-the-elements-that-are-the-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!