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: nabble.com/operation-with-if-else-on-a-dataframe-tp26109081p26109081.html Sent from the R help mailing list archive at Nabble.com.
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!! > > FrancescoFrancesco, 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