Mutate case_when - Remove negative results

Suppose that I have this chunk of code below that gives me some numerical results. How can I change this so that it will remove negative results (i.e., remove anything that's below zero)? That is, I only want results that are 0 or above.

Final_df <- Initial_df %>% 
  mutate(Result= case_when(
    `Type` == "A" ~ (Capability - Output)*(sum(Capability) - sum(Output)),
    `Type` == "B" | `Type` == "C" ~ (Forecast - Output),
    `Type` == "D" ~ Capability - Output)) 

Thank you,

If you want to remove rows where Result is less than zero, I suggest you add a filter step.

Final_df <- Initial_df %>% 
  mutate(Result= case_when(
    `Type` == "A" ~ (Capability - Output)*(sum(Capability) - sum(Output)),
    `Type` == "B" | `Type` == "C" ~ (Forecast - Output),
    `Type` == "D" ~ Capability - Output))  %>%
  filter(Result >= 0)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.