rpercentagemutated

Hos can I add a column porcentaje based on other column in my data frame?


I would like to create a column in my data frame that gives the percentage of each category. The total (100%) would be the summary of the column Score.

My data looks like

Client  Score
  <chr> <int>
1 RP      125
2 DM      30

Expected

Client  Score    %
  <chr> <int>
1 RP      125    80.6
2 DM      30     19.3

Thanks!


Solution

  • Note special character in column names is not good.

    library(dplyr)
    df %>% 
      mutate(`%` = round(Score/sum(Score, na.rm = TRUE)*100, 1))
    
      Client Score    %
    1     RP   125 80.6
    2     DM    30 19.4