Home > OS >  Case_when Mutate Function and making negative values zero
Case_when Mutate Function and making negative values zero

Time:06-11

Good day,

Suppose that I have this dataset that shows the capability and output of wind and gas generators, and it looks similar to this example table below:

Generation_Data = data.frame(Generator= c(1, 2, 3, 4, 5),
                     Fuel_Type = c('Gas', 'Wind', 'Wind', 'Gas', 'Wind'),
                     Available_Capacity = c(0, 11, 9, 0, 12 ),
                     Capability = c(50, 19, 18, 56, 20),
                     Output = c(50.5, 10, 7, 54, 10))

Note that "Available Capacity" for wind generators and "Capability" for gas generators essentially mean the same thing, and they represent the theoretical maximum amount of power the generators can produce.

I used mutate function below to calculate the difference between the theoretical generating capacity of each generator (i.e., "Available_Capacity" for wind generators and "Capability" for gas generators) and how much it actually generates (i.e., "Output").

library(dplyr)

Difference_Data <- Generation_Data %>% 
  mutate(Difference = case_when(
    Fuel_Type == 'Wind' ~ (Available_Capacity - Output),
    Fuel_Type == 'Gas' ~ (Capability - Output)
))

But I have these rare cases (Generator 1 in this example) where gas generators produce slightly more than their theoretical "Capability" and this causes the "Difference" to be negative. So I wanna add some codes that will make "Difference" for gas generators equal 0 if it's less than 0.

So in the aforementioned example, the Generator 1 has a difference of -0.5, but I want to make this equal 0 instead. Anyone know how to do this? Also, please note that I must do this without removing this "case_when" function!

Thank you in advance!

CodePudding user response:

One simple way is to just update the Difference column after the case_when() call, within the same mutate():

Difference_Data <- Generation_Data %>% 
  mutate(Difference = case_when(
    Fuel_Type == 'Wind' ~ (Available_Capacity - Output),
    Fuel_Type == 'Gas' ~ (Capability - Output)
  ),
  Difference=if_else(Difference<0,0,Difference)
  )

Output:

  Generator Fuel_Type Available_Capacity Capability Output Difference
1         1       Gas                  0         50   50.5          0
2         2      Wind                 11         19   10.0          1
3         3      Wind                  9         18    7.0          2
4         4       Gas                  0         56   54.0          2
5         5      Wind                 12         20   10.0          2

CodePudding user response:

We could do it this way:

As @langtang 1 already mentions, we could do it with an ifelse statement. Here we put it directly within the case_when statement, which I think is a little more interesting:

library(dplyr)

Difference_Data <- Generation_Data %>% 
  mutate(Difference = case_when(
    Fuel_Type == 'Wind' ~ (Available_Capacity - Output) ,
    Fuel_Type == 'Gas' ~ ifelse((Capability - Output) > 0, 
                                (Capability - Output), 0),
  ))
  Generator Fuel_Type Available_Capacity Capability Output Difference
1         1       Gas                  0         50   50.5          0
2         2      Wind                 11         19   10.0          1
3         3      Wind                  9         18    7.0          2
4         4       Gas                  0         56   54.0          2
5         5      Wind                 12         20   10.0          2
  •  Tags:  
  • r
  • Related