Forcing zero instead of NA in R -


i have function called newbamad , dataframe x. function matches letters in ref , alt columns , grabs respective numbers ref , alt values in x. need know how make function give 0 in ref or alt column instead of na. how replace na 0 here?

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")) {       # 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=','))     } 

we can use gsub that

 gsub('na', 0, newbamad(x)[,5]) 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -