我有一个数据框架,其中几个个体作为列,SNP作为行。每一列都有一个等位基因(例如G、A或N(如果不调用))。还有一些列包含每个SNP的主等位基因和(分离列)次等位基因。我尝试将单个等位基因值转换为基于主等位基因列和次要等位基因列的每个值的双向等位基因值(因此,如果个人的等位基因是主等位基因,我希望将次要等位基因粘贴在它后面,并用空格分隔,反之亦然)。如果值丢失(N),我想用0 0替换它。这里的想法是为Plink格式化这些数据。
到目前为止,我已经尝试使用ifelse函数,但没有成功。对于如何获得这里的双等位值有什么建议吗?非常感谢!我已经在我引用的格式中包含了一个虚构的数据集。
我现在所拥有的:
rs# major minor ind1 ind2 ind3 ind4
rs123456 A G A A A G
rs123457 G C C G C G
rs123458 C G C C G C
rs123459 T A A T N T我想做的是
rs major minor ind1 ind2 ind3 ind4
rs123456 A G A G A G A G G A
rs123457 G C C G G C C G G C
rs123458 C A C A C A A C C A
rs123459 T A A T T A 0 0 T A谢谢!抢夺
发布于 2017-10-10 21:01:06
这是做事情的一种方法。逐行浏览您的数据,从次要/主要中找到补语。请注意,您的输入和预期输出不匹配。
xy <- read.table(text = "rs major minor ind1 ind2 ind3 ind4
rs123456 A G A A A G
rs123457 G C C G C G
rs123458 C G C C G C
rs123459 T A A T N T", header = TRUE)
xy
out <- apply(xy, MARGIN = 1, FUN = function(x) {
findind <- grepl("^ind", names(x))
x[x %in% x["major"] & findind] <- paste(x[x %in% x["major"] & findind], x["minor"])
x[x %in% x["minor"] & findind] <- paste(x[x %in% x["minor"] & findind], x["major"])
x[x %in% "N"] <- "0 0"
list(x)
})
out <- sapply(out, "[", 1)
as.data.frame(do.call(rbind, out))
rs major minor ind1 ind2 ind3 ind4
1 rs123456 A G A G A G A G G A
2 rs123457 G C C G G C C G G C
3 rs123458 C G C G C G G C C G
4 rs123459 T A A T T A 0 0 T Ahttps://stackoverflow.com/questions/46666613
复制相似问题