Home > Software engineering >  Update column based on existing column and a condition in R
Update column based on existing column and a condition in R

Time:10-12

I am trying to mutate a column called Historic Penalty containing NA based on two columns, one with TRUE/FALSE named inspection count and other with a score named penalty.

    UPDATED_PENALTY['HISTORIC_PENALTY'] <- NA
 for (INSPECTION_CLEARED in TRUE) {
   mutate(HISTORIC_PENALTY = PENALTY*((0.8)^INSPECTION_COUNT))
   }

this is the code I am trying to run but I am getting the following error.

 Error in mutate(HISTORIC_PENALTY = PENALTY * ((0.8)^INSPECTION_COUNT)) : 
  object 'PENALTY' not found

Is there a better way of doing this or solving the error?

PENALTY column exist in my dataframe.

CodePudding user response:

Perhaps this helps

library(dplyr)
UPDATED_PENALTY <- UPDATED_PENALTY %>%
    mutate(
      HISTORIC_PENALTY = case_when(INSPECTION_CLEARED~ 
          PENALTY * ((0.8)^INSPECTION_COUNT), 
           TRUE ~ PENALTY))

In the OP's code, mutate was used without the dataframe object name

  •  Tags:  
  • r
  • Related