比较不同长度的多个向量,计算相同的元素,并打印出相同和不同的元素 [英] compare multiple vectors of different lengths, count the elements that are the same, and print out those that are the same and different

查看:82
本文介绍了比较不同长度的多个向量,计算相同的元素,并打印出相同和不同的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五个具有以下格式且长度不同的向量.是单核苷酸多态性(SNPs)的载体

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")

在R中,我想: 指出不同向量之间哪些SNP匹配 计算匹配的SNP数量 打印出哪些SNP不匹配 计算不匹配的SNP的数量

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

我发现以下脚本可以打印出矢量匹配的位置,但是我尝试了其中的一堆print和length函数,但似乎无法从中得到真正的希望.如果可能,我还想遍历我的五个向量(即A对B,A对C,A对D,A对E等).

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

推荐答案

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"

这篇关于比较不同长度的多个向量,计算相同的元素,并打印出相同和不同的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆