[R] operation with if/else on a dataframe

Fran100681 franaruto at hotmail.it
Thu Oct 29 09:47:17 CET 2009


Hi to all,
I have this dataframe (I show the first six rows)

>head(table)

 A                                           R    Fold.Change     P.Value 
Count1   Count2        
1 ENSRNOE00000000002_at         0         1.13             0.60        1      
1
2 ENSRNOE00000000009_at         0        -1.04             0.73        3      
3
3 ENSRNOE00000000020_at         0        -1.08             0.68        0      
0
4 ENSRNOE00000000021_at         0        -1.31             0.20        1      
2
5 ENSRNOE00000000023_at         0        -1.06             0.64        3      
3
6 ENSRNOE00000000024_at         0        -1.14             0.40        3      
3

I would like to generate a function that determine for each row a new value
(resulting in a new vector of values to add to the dataframe). 
The function should give for every row the same value showed in column "R".
However,I need of this because not all the R-values reported in this table
are correctly determined following the criteria mentioned below.


These new values calculated by the function must be:

1)"UP" 
if all the following conditions are verified in a certain row:
Fold.Change is >= +1.5, 
P.Value is < 0.05 
Count1 >= 2

2)"DOWN"
if all the following conditions are verified in a certain row:
Fold.Change is <= -1.5, 
P.Value is < 0.05 
Count2 >= 2

3) 0 if both of previous conditions are not verified

So I have set these fllowing parameters (new objects) because I'll have to
repeat this procedure to different dataframes in which the order of columns
of interest might change (So I can change these parameters depending on the
order of the columns in any different table)

Fold.change <-  3 #(because in this table, Fold.Change value is the third
column and so on...)
P.Value <-  4
Count1 <-  5
Count2 <-  6

The function is:

func_UP_0_DOWN <- function(x) {
if (x[Fold.Change] >= 1.5 & x[P.Value] <= 0.05 & x[Count1] >= 2) {
return ("UP")} else { 
if (x[Fold.Change] <= -1.5 & x[P.Value] <= 0.05 & x[Count2] >= 2) { 
return ("DOWN")} else {
return(0)} 
           } }

and then I have decided to apply the function for every row:

values.vector <- apply(table,1,func_UP_0_DOWN)

But when I check the resulting vector of calculated values:

> table(values.vector)
values.vector
     0 
192279 

I have all zero but this is wrong since I expect some "UP" and "DOWN"

However, if I apply this function on a single row in which I just know that
there is a "UP"-value...
(for example row 66):

>table[66,]
                                                R        Fold.Change  
P.Value  Count1  Count2
66 ENSRNOE00000000162_at        UP         1.84             0.01        3      
3

the function seems to work correctly:

> func_UP_0_DOWN(table[66,])
[1] "UP"

This is my problem, I cannot use the function to recalculate values in
R-column for all rows in my dataframe. I don't understand  where is the
problem, can someone help me?
Thanks a lot!!

Francesco
-- 
View this message in context: http://www.nabble.com/operation-with-if-else-on-a-dataframe-tp26109081p26109081.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list