Nono_sad
Nono_sad

Reputation: 443

R result of calculation not exceed threshold

It's maybe a stupid question but I cannot find the solution

I'm making a calculation and I don't want that the result exceed a maximum value

Let's say I don't want a value greater than 15

10+10= if( > 15) {15}

What is the correct syntax of this please?

Actually my problem is that I want to compute value and paste them using a if condition

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 :

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: 165

Answers (2)

Sathish
Sathish

Reputation: 12713

using ifelse()

x <- 1:20     

ifelse(x > 15, 15, x)
# [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 15 15 15 15 15

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 388982

You could use min or pmin based on your input type.

For scalar input :

min(5, 15)
#[1] 5
min(20, 15)
#[1] 15

For vector input :

pmin(11:17, 15)
#[1] 11 12 13 14 15 15 15

Upvotes: 4

Related Questions