Nono_sad
Nono_sad

Reputation: 443

R make calculation on if condition with paste

Actually my problem is that I want to compute value and paste them using a if condition. I'm computing a value that is proportional to a value in another column If condition is based on the fact that my value should not exceed 5 or be less than 5

r=0
g=0
b=0
activity_rgb_2=data.frame(activity=seq(from=-6,to = 6,by = 1))%>%
mutate(
  rgb=ifelse(activity<0,
             paste(r=-255*(activity)/5
                   if (r>255){r=255},0,0,sep = ","),paste(0,b=255*(activity)/5
                                                          if (b>255){r=255},0,sep = ",")))

I expect something like that :

activity_rgb
   activity     rgb
1        -6 255,0,0
2        -5 255,0,0
3        -4 204,0,0
4        -3 153,0,0
5        -2 102,0,0
6        -1  51,0,0
7         0   0,0,0
8         1  0,51,0
9         2 0,102,0
10        3 0,153,0
11        4 0,204,0
12        5 0,255,0
13        6 0,255,0 

I tried to do it using a function also

    scale_to_rgb <- function(val, colors = c("#ff0000","#000000", "#00ff00"), format = c("rgb", "comma")) {
      format = match.arg(format)
      val <- (val - min(val, na.rm = TRUE)) / diff(range(val, na.rm = TRUE))
      cols <- colorRamp(colors)(val)
      if (format == "rgb") {
        cols <- cols / 255
        rgb(cols[,1], cols[,2], cols[,3])
      } else {
        cols <- round(cols, 0)
        paste(cols[,1], cols[,2], cols[,3], sep = ",")
      }
    }

activity_rgb=data.frame(activity=seq(from=-4,to = 6,by = 1))%>%
  mutate(
    fake_activity = pmax(-5, pmin(activity,5)),
    palette = scale_to_rgb(fake_activity), # can be switch off
    rgb = scale_to_rgb(fake_activity, format = "comma") )

But the problem here is that I got 255,0,0 for value activity = -4 instead of 204,0,0

activity_rgb
   activity fake_activity palette     rgb
1        -4            -4 #FF0000 255,0,0
2        -3            -3 #C60000 198,0,0
3        -2            -2 #8E0000 142,0,0
4        -1            -1 #550000  85,0,0
5         0             0 #1C0000  28,0,0
6         1             1 #001C00  0,28,0
7         2             2 #005500  0,85,0
8         3             3 #008E00 0,142,0
9         4             4 #00C600 0,198,0
10        5             5 #00FF00 0,255,0
11        6             5 #00FF00 0,255,0

What I expect in this case is to got (palette is an extra column, it's not required):

activity_rgb
   activity fake_activity palette     rgb
1        -4            -4 #FF0000 204,0,0
2        -3            -3 #C60000 153,0,0
3        -2            -2 #8E0000 102,0,0
4        -1            -1 #550000  51,0,0
5         0             0 #1C0000  0,0,0
6         1             1 #001C00  0,51,0
7         2             2 #005500  0,102,0
8         3             3 #008E00 0,153,0
9         4             4 #00C600 0,204,0
10        5             5 #00FF00 0,255,0
11        6             5 #00FF00 0,255,0

Thanks

Upvotes: 0

Views: 42

Answers (1)

sachin2014
sachin2014

Reputation: 462

I think we can use ifelse within the paste statement as shown below:

activity_rgb_2=data.frame(activity=seq(from=-6,to = 6,by = 1))%>%
  mutate(rgb=ifelse(activity<0, paste(ifelse(-255*(activity)/5 > 255, 255, -255*(activity)/5),0,0,sep = ","),
                    paste(0, ifelse(255*(activity)/5 > 255, 255,255*(activity)/5),0,sep=",")))

Output

   activity     rgb
1        -6 255,0,0
2        -5 255,0,0
3        -4 204,0,0
4        -3 153,0,0
5        -2 102,0,0
6        -1  51,0,0
7         0   0,0,0
8         1  0,51,0
9         2 0,102,0
10        3 0,153,0
11        4 0,204,0
12        5 0,255,0
13        6 0,255,0

Upvotes: 1

Related Questions