convert character to percentage R [duplicate]

Does this work:

library(dplyr)
library(readr)
df %>% mutate(D = as.numeric(parse_number(Discount))) %>% mutate(Discount_Amount = case_when(is.na(D) ~ 0, TRUE ~ (D/100)*Price)) %>% select(-D)
  Discount Price Discount_Amount
1      10%    10             1.0
2             20             0.0
3       5%    30             1.5

Data used:

df
  Discount Price
1      10%    10
2             20
3       5%    30
> 

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top