Home > Back-end >  How to calculate percentage for each column of a dataframe based on the total given in another colum
How to calculate percentage for each column of a dataframe based on the total given in another colum

Time:08-09

I want to calculate percentage of each column in a dataframe by adding new column after each column in R. How can I achieve this

The percentage is calculated based on the another column.

Here's is an example of a dataset. Percentage is calculated based on total column for col2, col3, col4


Var  total col2 col3 col4
A    217   77   62   78
D    112   14   47   51
B    91    15   39   37
R    89    77   7    5
V    80     8   53   19

The output should look like

Var  total col2 col2_percent  col3 col3_percent col4  col4_percent
A    217   77        35.48%    62      28.57%     78     35.94%
D    112   14        12.50%    47      41.96%     51     45.54%
B    91    15        16.48%    39      42.86%     37     40.66%
R    89    77        86.52%    7       7.87%      5      5.62%
V    80     8        10.00%    53      66.25%     19     23.75%

CodePudding user response:

You can use across:

library(dplyr)

df %>% 
  mutate(across(-c(Var, total), ~ sprintf('%.2f%%', .x / total * 100), .names = "{col}_percent")) %>% 
  relocate(Var, total, sort(colnames(.)))

  Var total col2 col2_percent col3 col3_percent col4 col4_percent
1   A   217   77       35.48%   62       28.57%   78       35.94%
2   D   112   14       12.50%   47       41.96%   51       45.54%
3   B    91   15       16.48%   39       42.86%   37       40.66%
4   R    89   77       86.52%    7        7.87%    5        5.62%
5   V    80    8       10.00%   53       66.25%   19       23.75%
  • Related