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!