[R] operation with if/else on a dataframe

2009-10-29 Thread Fran100681

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

head(table)

 A   RFold.Change P.Value 
Count1   Count2
1 ENSRNOE002_at 0 1.13 0.601  
1
2 ENSRNOE009_at 0-1.04 0.733  
3
3 ENSRNOE020_at 0-1.08 0.680  
0
4 ENSRNOE021_at 0-1.31 0.201  
2
5 ENSRNOE023_at 0-1.06 0.643  
3
6 ENSRNOE024_at 0-1.14 0.403  
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,]
RFold.Change  
P.Value  Count1  Count2
66 ENSRNOE162_atUP 1.84 0.013  
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.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] operation with if/else on a dataframe

2009-10-29 Thread Bernardo Rangel Tura
On Thu, 2009-10-29 at 01:47 -0700, Fran100681 wrote:
 Hi to all,
 I have this dataframe (I show the first six rows)
 
 head(table)
 
  A   RFold.Change P.Value 
 Count1   Count2
 1 ENSRNOE002_at 0 1.13 0.601  
 1
 2 ENSRNOE009_at 0-1.04 0.733  
 3
 3 ENSRNOE020_at 0-1.08 0.680  
 0
 4 ENSRNOE021_at 0-1.31 0.201  
 2
 5 ENSRNOE023_at 0-1.06 0.643  
 3
 6 ENSRNOE024_at 0-1.14 0.403  
 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

[ Quote text]

 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

Francesco,


I think you solve this problem with a simple way.
Remember in R the most function and operations are vectorized so look
this example: 

set.seed(123)
x-rpois(20,5)
y-rpois(20,15)
z-rpois(20,10)
dta-data.frame(x,y,z)
dta
dta$NEW-ifelse(x5  y15  z10,UP,
ifelse(x5  y15  z10,DOWN,
0))
dta

First, I use ifelse command to simplify your nested conditional
situation.

Second, I know that R test this nested condition in order so the first
position will result test x[1],y[1] and z[1], the second postion will
result test x[2],y[2] and z[2] ...

The new vector result is the same order the original data.frame so I use
dta$NEW to create a new column in data.frame 



-- 
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] operation with if/else on a dataframe

2009-10-29 Thread Fran100681

 Ok, thank you a lot!!


Bernardo Rangel tura wrote:
 
 On Thu, 2009-10-29 at 01:47 -0700, Fran100681 wrote:
 Hi to all,
 I have this dataframe (I show the first six rows)
 
 head(table)
 
  A   RFold.Change P.Value 
 Count1   Count2
 1 ENSRNOE002_at 0 1.13 0.601 
  
 1
 2 ENSRNOE009_at 0-1.04 0.733 
  
 3
 3 ENSRNOE020_at 0-1.08 0.680 
  
 0
 4 ENSRNOE021_at 0-1.31 0.201 
  
 2
 5 ENSRNOE023_at 0-1.06 0.643 
  
 3
 6 ENSRNOE024_at 0-1.14 0.403 
  
 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
 
 [ Quote text]
 
 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
 
 Francesco,
 
 
 I think you solve this problem with a simple way.
 Remember in R the most function and operations are vectorized so look
 this example: 
 
 set.seed(123)
 x-rpois(20,5)
 y-rpois(20,15)
 z-rpois(20,10)
 dta-data.frame(x,y,z)
 dta
 dta$NEW-ifelse(x5  y15  z10,UP,
 ifelse(x5  y15  z10,DOWN,
 0))
 dta
 
 First, I use ifelse command to simplify your nested conditional
 situation.
 
 Second, I know that R test this nested condition in order so the first
 position will result test x[1],y[1] and z[1], the second postion will
 result test x[2],y[2] and z[2] ...
 
 The new vector result is the same order the original data.frame so I use
 dta$NEW to create a new column in data.frame 
 
 
 
 -- 
 Bernardo Rangel Tura, M.D,MPH,Ph.D
 National Institute of Cardiology
 Brazil
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 
 

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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.