-1

I have a table in R and would like to calculate the percentage by each column i.e what percent of X is low?

transaction <-

enter image description here

Rick2ja
  • 27
  • 4
  • 2
    Please include code, and data, as [plain text](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), not images which users cannot copy/paste. – neilfws Apr 05 '19 at 02:44
  • 1
    take a look at `prop.table()` and apply it rowwise. – boski Apr 05 '19 at 06:32

1 Answers1

0

If its already a table you only need to take bottom line from the following code, if however it is a data.frame you will need to convert it.

df <- data.frame(row.names = c("High", "Low", "Med"),
             A = c(0,905136,0),
             B = c(0,978531,379375),
             C = c(471235,2059469,8104087),
             D = c(244216,2873406,9842409)
             )

df <- as.table(as.matrix(df))

prop.table(df, 2)

Outputs the following:

              A          B          C          D
High 0.00000000 0.00000000 0.04431070 0.01884378
Low  1.00000000 0.72061763 0.19365392 0.22171290
Med  0.00000000 0.27938237 0.76203538 0.75944332
Aaron Walton
  • 150
  • 1
  • 10