1

I have seen solutions using ifelse for working on vectors. I have more details that need to be addressed in my case. While I am trying my best to figure out different ways to work on it by either declaring functions for yes and no conditions and using it in ifelse or trying to use nested ifelse loops, I am not sure what I am missing because I am no getting the results I need. So I am bringing it all the R experts here. Thanks for taking your time and helping me.

my input data

two.mz=57   
two.rt=16


x.mz

0       57      0   0   0   
2   0   0.00000 0   0   0   
3   0   0.00000 0   0   0   
4   0   0.00000 0   0   0   
5   0   0.00000 0   0   0   
6   0   0.00000 0   0   0   
7   0   0.00000 0   0   0   
8   0   0.00000 0   0   0   
9   0   0.00000 0   0   0   

y.rt   
0   16  0   0   0   
2   0   0.00000 0   0   0   
3   0   0.00000 0   0   0   
4   0   0.00000 0   0   0   
5   0   0.00000 0   0   0   
6   0   0.00000 0   0   0   
7   0   0.00000 0   0   0   
8   0   0.00000 0   0   0   
9   0   0.00000 0   0   0  



mz.test        
2   TRUE       
3   FALSE   
4   FALSE   
5   FALSE   
6   FALSE  
7   FALSE  
8   FALSE  
9   FALSE  
10  FALSE

rt.test is also a logical matrix. mz.test and rt.test will be of the same dimension.

I need to check a simple condition: if mz.test is T and if rt.test is also T in the corresponding row, then perform a set of actions. If they are not *both TRUE in all corresponding rows in both matrices*, then insert a row in the main matrix with values. I use this function for doing the insertion:

insert.under.row.number <- function (mat, rnum, what) 
{    
   if (length(what) != ncol(mat)) what <- c(0,what, rep_len(NA, ncol(mat) - length(what)))    
   rbind(mat[1:(rnum-1), ], what,mat[(rnum + 1):nrow(mat), ])   
}

I got help for this part also from this site. :)

My code snippet:

if (mz.test==T & rt.test==T)
{
  count=count+1
  x.mz[j,2]=two.mz  ##x.mz is a main matrix  ###two.mz and two.rt are single numeric values
  y.rt[j,2]=two.rt  ##y.rt is another main matrix
} else {
  count1=count1+1
  newrow.m=two.mz
  newrow.r=two.rt 
  x.mz <- insert.under.row.number(x.mz, j,newrow.m)
  row.names(x.mz) <- 1:nrow(x.mz1)
  y.rt <- insert.under.row.number(y.rt, j,newrow.r)
  row.names(y.rt) <- 1:nrow(y.rt1)
  print(count1)
}

I have shown if () here because I am not getting the structure right for ifelse(). Can anyone help me with my mistakes here? Thanks a lot!

Roland
  • 127,288
  • 10
  • 191
  • 288
  • 2
    Please show examples for all input data and show the expected result. See [this FAQ](http://stackoverflow.com/a/5963610/1412059) for help on how to do that. – Roland Dec 05 '13 at 08:08
  • 1
    You are welcome. I was going to post the same link as above. By providing the data (or some code to generate it), you'll make it possible for us to run your code and see what can be done. – tonytonov Dec 05 '13 at 08:19
  • Are both `mz.test` and `rt.test` one-column matrices with the same number of rows? Do `x.mz` and `y.rt` also have the same number of rows like the one-column matrices? – Sven Hohenstein Dec 05 '13 at 08:49
  • I have included the input files in my Q above. – user2698508 Dec 05 '13 at 09:10
  • @SvenHohenstein- Yes. They are one column matrices with T or F values only. Yes, x.mz and y.rt are not one column matrices. These are the empty matrices I created to store the results from the if statements. In the edit above, I have shown one value added to [1,2] in both. This means that mz.test and rt.test both have TRUE at [1,2]. Am I any clearer? – user2698508 Dec 05 '13 at 09:14
  • @Roland, Thanks for the editing. The input matrices are more readable now. :) – user2698508 Dec 05 '13 at 10:00
  • @user2698508 It's very polite to thank for editing, but I would prefer if you tried to do it yourself. It's really easy. Have a look at the buttons on top of the edit window and look at the text appearing if you hover the mouse pointer over these buttons. I used the "Code Sample" button. – Roland Dec 05 '13 at 10:13
  • What is the"main matrix" ? please identify each step with the actual object name. – Carl Witthoft Dec 05 '13 at 12:23
  • I reworked the logic since this was proving very difficult for me to manage. I have some difficulties in that approach. I will post it as a new question so that it is not confusing here. I will add the link to that Question below. – user2698508 Dec 12 '13 at 08:06
  • lInk to the Question.Thanks for all your help. http://stackoverflow.com/questions/20538518/row-indexes-of-last-element-added-to-a-zeros-matrix-in-r – user2698508 Dec 12 '13 at 08:34

0 Answers0