Nested if-else statement in R
2
0
Entering edit mode
7.6 years ago
M K ▴ 660

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"))).

R • 16k views
ADD COMMENT
3
Entering edit mode
7.6 years ago
ATpoint 81k

The problem with "ifelse" in R is, that it takes an expression but returns values. It does not run a new statement.

ifelse(1<2, 5, 2) is ok and will prompt out 5, but ifelse(1<2, cat("Cool"), cat("foo")) returns an error.

Instead, use the if and else statements like this:

  if (data < 0.01){
    cat("Variant1")
  } else {
       if (data >= 0.01 & data < 0.5){
         cat("Variant2")
       } else {
           cat("Variant3")
         }
    }

Also be careful about your use of operators like > and < because in your current code examples, the case that data is exactly 0.01 is ignored. You only defined conditions for it being smaller or larger.

ADD COMMENT
0
Entering edit mode

Is there a reason why you are using

 else {
   if () {}
}

instead of else if () {}

ADD REPLY
0
Entering edit mode

I like structuring my code using (whitespace) indentation to have a better overview of where a block begins and ends. But this is a matter of taste, you can also put in all in one line. Unlike languages such as Python, R does not use strict indentations, so you are free to do it as you like.

ADD REPLY
1
Entering edit mode
7.6 years ago
biocyberman ▴ 860

This should work:

data <- c(0.001, 0.02, 0.06)
col=ifelse(data < 0.01,"green" , ifelse(data > 0.01 & data < 0.05, "red", ifelse(data>0.05, "yellow", "unknown")))
ADD COMMENT
0
Entering edit mode

Here I tried your function with my data and it works,but the colors are not matched the condition as you can see in the plot below

Here is my r code and the dataset:

  map_pv = read.csv("data1.csv",header=TRUE, sep=",")                 
  head(map_pv)
  dim(map_pv)
 rownames(map_pv)
 row.names(map_pv) <- map_pv$X
 rownames(map_pv)
colnames(map_pv) 
map_pv <- map_pv[,2:4]
colnames(map_pv) 
map_pv <- data.matrix(map_pv)
library(gplots)
 heatmap.2(map_pv, trace="none",
  margins=c(8,10),keysize=1,Rowv=NULL, Colv=NULL, scale="none",
  col=ifelse(map_pv< 0.01,"green" , ifelse(map_pv>= 0.01 & map_pv<= 0.05, "yellow", ifelse(map_pv>0.05, "red",    
  "pink"))),  cexRow=1.5,cexCol=1.5,
  cellnote = ifelse(heat_map_pv< 0.01, "***",ifelse(map_pv > 0.01 & map_pv <0.05, "**",".")),
  notecol = "black", density.info = "none", srtCol=0)


                   var1      var2      var3
       x1          0            0          0.0338
       x2          0            0          0.3773
      x3           0.02357  0.0004   0.5143
      x4          0.86671   0.95087  0.9448
      x5          0.00485   0.99488  0.0392
      x6           0.0794   0.30871  0.0287
      x7          0.76839   0.31633  0.5311
      x8          0.00167   0.00001   0.812
      x9          0.01882   0.17083   0.583
     x10          0.82622   0.23251 0.8044
     x11          0.81513   0.73145 0.8906
     x12        0.01163 0.8119  0.0496
     x13          0.03327   0.12126 0.5562
     x14          0.00001   0           0
     x15          0.00221   0.22781 0.6467
     x16          0.0426            0               0.2918
     x17          0.35641   0.70819 0.3824
     x18          0.07151   0.84156 0.2592
     x19          0.01226   0.72313 0.5072
     x20          0.247         0.51079   0.389
     x21          0.03505   0           0.0579
     x22          0.73015   0.66604 0.4537
     x23          0.01343   0.14461 0.0336
ADD REPLY
0
Entering edit mode

I suggest to test with one column of data first. You are trying to do many things at once. That makes it difficult to see why you are not getting what you expect. My suspicion is that you are having the col as a matrix, that makes it incompatible with heatmap.2 (just my guess, I don't use it myself). Also try peeking into col to see if you get expected values.

Check these out for inspirations:

http://stackoverflow.com/questions/20535635/how-to-assign-your-color-scale-on-raw-data-in-heatmap-2

http://sebastianraschka.com/Articles/heatmaps_in_r.html

ADD REPLY

Login before adding your answer.

Traffic: 2260 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6