Mutate - Set results to zero if it's negative

Good day,

I posted a similar question before and asked how to remove negative values from the code chunk below. FJCC kindly told me to put "filter(Result >= 0)" at the end of the code to get rid of them, and it worked well!

But now, instead of completely removing them, I want to set the negative values to zero. For example, if Result = -26, then I want my code to change that to Result = 0 for all negative observations for the column Result.

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!

add another mutate for Result … ifelse(Result < 0,0,Result)


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.