0

I want to construct a 2x2 contingency table to compute the odds ratio. However, I fail to sum up the rows and columns as I wanted them to be. This is my contingency table for now. I want to total up Down Steady and Up into a new variable named Yes.

|        |Down|No|Steady|Up|
|--------|----|--|------|--|
|Female  |    |  |      |  |
|Male    |    |  |      |  |
|Female.1|    |  |      |  |
|Male.1  |    |  |      |  |
|Female.2|    |  |      |  |
|Male.2  |    |  |      |  |

I have tried using colsums but it just totals up the entire column. The output I want is like this:

|      |Yes|No|
|------|---|--|
|Female|   |  |
|Male  |   |  |

Thank you in advance!

  • Is it a matrix or data.frame – akrun Apr 02 '21 at 01:10
  • I have converted it into data.frame. – Jenny Empawi Apr 02 '21 at 01:11
  • May be you need to remove the digits and dot in the row names. `grp <- sub("\\.\\d+", "", rownames(df1)); rowsum(df1[c('Down', 'Steady')], grp); and rowsum(df1[c(''No", "Up")], grp)` – akrun Apr 02 '21 at 01:16
  • This gives me this result: ```Down Steady Female 6895 26843 Male 5909 23019``` but i want down,steady up one group and no as another group. – Jenny Empawi Apr 02 '21 at 01:21
  • I tried this and it works! ```grp <- sub("\\.\\d+", "", rownames(df1)); rowsum(df1[c('No','Up')], grp); and rowsum(df1[c("Steady","Up","Down")], grp)``` – Jenny Empawi Apr 02 '21 at 01:25
  • However, R gives me error like this: Error: unexpected symbol in " and rowsum" – Jenny Empawi Apr 02 '21 at 01:28
  • It is a `base R` function `?rowsum` i.e. `rowsum(mtcars[1:2], mtcars$am)` – akrun Apr 02 '21 at 01:29
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Apr 02 '21 at 02:52

0 Answers0