0

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
   .......

1 Answers1

1

No need to use ifelse. Your statement is equivalent to (x > 3) - (x < -3).

So you could just use data.table:

library(data.table)
d <- fread("input.txt")

d[ , lapply(.SD, function(x) (x > 3) - (x < -3))]
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • Input A1 A2 B2 B3 B5 1: 1 1 0 0 1 0 2: 1 1 0 0 0 0 3: 1 1 0 0 0 0 4: 1 1 1 0 0 0 5: 1 1 0 0 0 0 few lines of output when running this..i miss the first column – user2243831 Mar 04 '16 at 03:07
  • @user2243831 you cant honestly expect me to parse that. please consider making your question [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MichaelChirico Mar 04 '16 at 03:14
  • I edited the question..when i run your program i am getting all 1's in Input column not a1,a2 etc. – user2243831 Mar 04 '16 at 03:29