我有一个名为newBamAD
和数据框x
的函数。此函数的作用是匹配REF和ALT列中的字母,并获取REF
中ALT
和x
值的相应数字。我需要知道的是如何使这个函数在ref或alt列而不是NA中给出0。如何在此处将NA替换为零?
x <- as.matrix(read.csv(text="start,A,T,G,C,REF,ALT,TYPE
chr20:5363934,95,29,14,59,C,T,snp
chr5:8529759,,,,,G,C,snp
chr14:9620689,65,49,41,96,T,G,snp
chr18:547375,94,1,51,67,G,C,snp
chr8:5952145,27,80,25,96,T,T,snp
chr14:8694382,68,94,26,30,A,A,snp
chr16:2530921,49,15,79,72,A,T,snp:2530921
chr16:2530921,49,15,79,72,A,G,snp:2530921
chr16:2530921,49,15,79,72,A,T,snp:2530921flat
chr16:2530331,9,2,,,A,T,snp:2530331
chr16:2530331,9,2,,,A,G,snp:2530331
chr16:2530331,9,2,,,A,T,snp:2530331flat
chr16:2533924,42,13,19,52,G,T,snp:flat
chr16:2543344,4,13,13,42,G,T,snp:2543344flat
chr16:2543344,42,23,13,42,G,A,snp:2543344
chr14:4214117,73,49,18,77,G,A,snp
chr4:7799768,36,28,1,16,C,A,snp
chr3:9141263,27,41,93,90,A,A,snp", stringsAsFactors=FALSE))
newBamAD <- function (x,base.types=c("A","C","G","T")) {
# the version above
rownames(x) <- 1:nrow(x)
ref <- x[cbind(1:nrow(x), x[, 'REF'])]
alt <- x[cbind(1:nrow(x), x[, 'ALT'])]
which.flat <- grep('flat$', x[, 'TYPE'])
alt[which.flat] <- sapply(which.flat, function (i,base.types) {
sum(as.numeric(x[i, c( base.types[!( base.types %in% x[i, 'REF'])] )] ) ,na.rm=TRUE) },base.types)
cbind(x[,c("start","REF","ALT","TYPE")],bam.AD=paste(ref, alt, sep=','))
# cbind(x, bam.AD=paste(ref, alt, sep=','))
}
答案 0 :(得分:4)
您可以接受thelatemail的建议并切换到数据框,然后先取出NA
df <- as.data.frame(x)
types <- c("A", "T", "G", "C")
df[types][is.na(df[types])] <- 0
head(newBamAD(df))
# start REF ALT TYPE bam.AD
# 1 chr20:5363934 C T snp 59,29
# 2 chr5:8529759 G C snp 0, 0
# 3 chr14:9620689 T G snp 49,41
# 4 chr18:547375 G C snp 51,67
# 5 chr8:5952145 T T snp 80,80
# 6 chr14:8694382 A A snp 68,68
答案 1 :(得分:1)
我们可以使用template
来执行此操作
gsub