gpt4 book ai didi

r - 基于 r 中的另一个向量为向量中的每个元素排序字母

转载 作者:行者123 更新时间:2023-12-01 13:54:38 24 4
gpt4 key购买 nike

我有一个矢量看起来像:

SNP <- c("A/G","A/C","A/C","T/C","A/G","A/C","T/G","T/C","A/C","T/G")

第二个向量 (REF) 是“SNP”中每个元素的两个字母之一的向量:

REF <- c("G","C","A","C","A","A","T","T","C","T")

如果匹配向量“REF”中的相应元素(字母),我想将向量“SNP”中每个元素的任何字母放在前面。预期结果是:

SNP <- c("G/A","C/A","A/C","C/T","A/G","A/C","T/G","T/C","C/A","T/G")

最佳答案

你可以试试:

mapply(function(x,y) paste(y,setdiff(x,y),sep="/"),
strsplit(SNP,"/",fixed=TRUE),REF)
#[1] "G/A" "C/A" "A/C" "C/T" "A/G" "A/C" "T/G" "T/C" "C/A" "T/G"

关于r - 基于 r 中的另一个向量为向量中的每个元素排序字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35462222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com