Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update column based on existing column and a condition in R

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

PENALTY column exist in my dataframe.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading