[R] operation with if/else on a dataframe

Fran100681 franaruto at hotmail.it
Thu Oct 29 10:56:43 CET 2009


 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                                           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
> 
> [ 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(x>5 & y>15 & z>10,"UP",
> ifelse(x<5 & y<15 & z<10,"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 at 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.




More information about the R-help mailing list