I am trying to convert this table into percentage table columnwise.
| Class | Jan2021 | Feb 2021 |
|---|---|---|
| A | 50 | 100 |
| B | 50 | 150 |
| C | 100 | 100 |
I am expecting this table.
| Class | Jan2021 | Feb 2021 |
|---|---|---|
| A | 25% | 28.57% |
| B | 25% | 42.86% |
| C | 50% | 28.57% |
x <- data.frame(class = c("A", "B", "C"),
Jan2021 = c(50,50,100),
Feb2021 = c(100,150,100))
I could do this, but I have lots of columns/rows with different names. Is there an easier way to calculate the percentage for each column?
x_pct = mutate(x,
Jan_pct = Jan2021 / sum(Jan2021) *100,
Feb_pct = Feb2021 / sum(Feb2021) *100)