Dear expert, How can I make follow codematrix function more faster?
top20.dat <- top20.dat[,7:length(top20.dat[1,])] top40.dat <- read.table("top40snps.ped",header=F,sep="\t",stringsAsFactors=F) row.names(top40.dat) <- top40.dat[,1] top40.dat <- top40.dat[,7:length(top40.dat[1,])] codematrix <- function(dat) { new.dat <- dat for(col in 1:length(dat[1,])) { tbl <- table(dat[,col]) max.allel <- names(which(tbl==max(table(dat[,col])))) for(row in 1:length(dat[,1])) { if(dat[row,col]=="0 0") { new.dat[row,col]=NA }else{ if(dat[row,col]==max.allel) { new.dat[row,col]=0 }else{ allele <- unlist(strsplit( as.character(dat[row,col])," ")) if(allele[1]==allele[2]){ new.dat[row,col]=2 }else{ new.dat[row,col]=1 } } } } #}) cat(paste(col," ",sep="")) } return(new.dat) } code.top20 <- codematrix(top20.dat) [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.