I am trying to read a huge matrix file in R and using ifelse to replace values with some conditions and having issues
INPUT
Input A1 A2 B2 B3 B5
a1 7.805 0 2.722 3.601 1.106
a2 6.557 0 2.449 2.828 -2
a3 5.899 2.333 2 0 0
a4 5.779 7.517 0 0 0
a5 5.52 0 1.633 1.342 0
.......
.......
a300 -6.172 0 0 0 0.917
a301 -6.24 -2.132 -2.218 -1.973 0
a302 -6.436 0 0 1.516 4.611
a303 -6.529 0 0 0 0
a305 -6.607 -2.668 -2.883 0 0
i tried running below lines but in output i see its changing the conditions for some number of lines(random columns) and not all of them and row names are changing
d <- read.table("input.txt",header=T,sep="\t",row.names=1,as.is=TRUE)
s <- apply(d,2,function(x) ifelse(x>=3,1, ifelse(x<=-3,-1, ifelse(x<3|x>-3,0,x))) )
write.table(s,file="s.txt",sep="\t",quote=FALSE)
OUTPUT OF ABOVE SCRIPT
A1 A2 B2 B3 B5
a1 1 0 0 1 0
a2 1 0 0 0 0
a3 1 0 0 0 0
a4 1 1 0 0 0
a5 1 0 0 0 0
.....
.....
a300 -6.172 0 0 0 0.917
a301 -6.24 -2.132 -2.218 -1.973 0
a302 -6.436 0 0 1.516 4.611
a303 -6.529 0 0 0 0
a305 -6.607 -2.668 -2.883 0 0
can i get help in this?
Expected output
Input A1 A2 B2 B3 B5
a1 1 0 0 1 0
a2 1 0 0 0 0
a3 1 0 0 0 0
a4 1 1 0 0 0
a5 1 0 0 0 0
.......