How to get the sum of two observations in one variable

Hi Guys

I Have 3 column, Merchant RGP, Facevalue, Status,

Merchant RGP FaceValue Status
MAY & 1500 Successful
MAY & 1500 Failed
MAY & 1500 Successful
Markers 1500 Failed
Markers 1500 Failed
Markers 1500 Successful
Markers 241 Failed
Bloosom 1500 Failed
Bloosom 1501 Failed
Flutters 300 Successful
Flutters 1000 Failed
Flutters 200 Failed
Flutters 2000 Successful
Flutters 200 Successful
Flutters 5100 Failed
Pecks 100 Successful
Pecks 200 Successful
Pecks 500 Failed
Pecks 300 Failed
Pecks 100 Successful

The Status column has been grouped into two observations eg successful and failed
in my code, I have grouped by the merchant RGP.

What I want is that I want to create or mutate and give me the sum of successful value for each merchant and also give me sum of failed value for each merchant
and create another column and give me the percentage of successful for each merchant and repeat the same for failed

but I didn't get the result
it keeps returning zeros for each merchant
you can help me add or edit my code and write a new one for me probably learn from it

here is my code

April_updated_new_stylea %>%
group_by(Merchant_RGP) %>%
summarise(Successful_rate = sum(Status == "Successful"),
failure_rate = sum(Status != "Successful" ))%>%
mutate(Successful_rate_pct = Successful_rate/sum(Successful_rate),
Failure_rate_pct = failure_rate/sum(failure_rate),
Success_Pct = round(Successful_rate_pct , digits = 2),
Failure_Pct = round(Failure_rate_pct, digits = 2)

Thank you


To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:


Thanks for the feedback

i just added some few lines for reprex as you suggested

your feedback is appreciated

Hi friend, sorry, you must have missed the part where practical advice was given around how to share an example data.frame you prepared. Look for dput() and also how to format text as code for the forum. It's all there, keep going with it, it will pay off :slight_smile:

The challenge is that Successful and Failed cannot be summed. You need to make a column that can be summed from that.

I think you're looking for something like

df %>% 
  mutate(stat = ifelse(status == "Successful", 1, 0)) %>% 
  group_by(merchant, stat) %>% 
  mutate(n_stat = sum(stat)) %>% 
  group_by(merchant) %>% 
  mutate(x = n_stat / sum(n_stat))

This topic was automatically closed 21 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.