0

I have a file contains p-values of my analysis. I want to use if else statement to do the following:

if p-value less that 0.01 give a green color. if p-value is greater than 0.01 and less that 0.05 give a red color. if p-value greater than 0.05 give a yellow color.

I tried to use the following code but is doesn't work:

col=ifelse(data < 0.01,"green" , 
           ifelse(data > 0.01 & data < 0.05, "red"), 
                  ifelse(data>0.05, "yellow"))).
Psidom
  • 209,562
  • 33
  • 339
  • 356
Mark K.
  • 67
  • 7
  • What do you mean by "doesn't work". Give any error messages exactly as they appear. You should provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with your question. The `ifelse()` function requires both an `if` and `else` part. Looks like your "yellow" part is missing the "else" – MrFlick Sep 25 '16 at 02:15
  • Also, looks like the `red` part closes incorrectly. – Ronak Shah Sep 25 '16 at 02:16
  • 2
    ifelse is a vectorized function, not intended for this kind of nesting if else. Just use regular if, else if, else if, etc since you don't have vectorize needs. – dracodoc Sep 25 '16 at 02:17
  • It works, but it gave the same color for the second and third condition. – Mark K. Sep 25 '16 at 02:17
  • Your parenthesis have problem. if "green" is the yes condition of the outmost ifelse, the 2nd ifelse should wrap until end. like this `ifelse(data < 0.01,"green" , ifelse(data > 0.01 & data < 0.05, "red", ifelse(data>0.05, "yellow")))`. Here you really should use nested if, else if, else if, ... – dracodoc Sep 25 '16 at 02:39

2 Answers2

3

In this case, ifelse is a poor solution to the problem. It sounds like you are trying to take a set of continuous values (the p-values) and generate discrete labels for them. R's base cut function is designed for exactly this purpose. Suppose we have:

example.data <- data.frame(p = c(0.0001, 0.001, 0.01, 0.025, 0.5))

       p
1 0.0001
2 0.0010
3 0.0100
4 0.0250
5 0.5000

We can simply use cut to generate the labeling you want:

example.data$color <- cut(example.data$p, breaks = c(0, 0.01, 0.05, 1), labels = c('green', 'red', 'yellow'))

       p  color
1 0.0001  green
2 0.0010  green
3 0.0100  green
4 0.0250    red
5 0.5000 yellow
jdobres
  • 11,339
  • 1
  • 17
  • 37
-1
col=ifelse(data < 0.01,"green" , 
           ifelse(data > 0.01 && data < 0.05, "red"), 
                  ifelse(data>0.05, "yellow"))).

/** There's some things here that is just basics: it's much easier for you to create a Method (don't need to be the name i have set) that does the work.

 you should use the else's and if's as following:

 if(condition){command}
 else if (another condition){command} 
 else{command that runs if no conditions are triggered}


 notice that you can use as many else if() as needed as long as you finish using else and only else */


public void changeColor(){
if(data<0.01)
col = "green"
else if(data>0.01 && data <0.05)
col= "red"
else{col="yellow"}

}

i hope this was of some help, i'm also only a beginner.

Rutzen
  • 1
  • 2
  • 1
    This is an R question – Rich Scriven Sep 25 '16 at 02:31
  • Hi, Rutzen, I run your function and it worked but it didn't give any results. – Mark K. Sep 25 '16 at 03:04
  • it's because i gave you only the method, you need to adapt him to fit your class – Rutzen Sep 25 '16 at 03:58
  • That's what I did: changeColor<-function(){ if(data<0.01) col = "green" else if(data_pv>0.01 &&data_pv<0.05) col= "red" else{col="yellow"} } and it doesn't work. My data is a matrix of p value. Is this cause that problem. – Mark K. Sep 25 '16 at 04:08