How to calculate row-wise percentages using the tableby function in arsenal package or anything that would produce similar results


#1

#How to calculate row-wise percentages in a using the arsenal package
#The code below produces col-wise percentages, but i would like to produce row-wise percentages. 

library(arsenal)
library(forcats)

 df <-  data.frame(arm = sample(c("Int","Contr"),size=10, replace = T), 
                  sex = sample(c("Male", "Female"),size=10, replace = T),
                 agegp = sample(c("<15","15-49",">50"),size=10, replace = T))

tab1 <- tableby(arm ~ sex + agegp, data=df)

summary(tab1, text=TRUE)
#> 
#> 
#> |          | Contr (N=7) | Int (N=3)  | Total (N=10) | p value|
#> |:---------|:-----------:|:----------:|:------------:|-------:|
#> |sex       |             |            |              |   1.000|
#> |-  Female |  4 (57.1%)  | 1 (33.3%)  |  5 (50.0%)   |        |
#> |-  Male   |  3 (42.9%)  | 2 (66.7%)  |  5 (50.0%)   |        |
#> |agegp     |             |            |              |   0.040|
#> |-  <15    |  1 (14.3%)  | 3 (100.0%) |  4 (40.0%)   |        |
#> |-  >50    |  3 (42.9%)  |  0 (0.0%)  |  3 (30.0%)   |        |
#> |-  15-49  |  3 (42.9%)  |  0 (0.0%)  |  3 (30.0%)   |        |

Created on 2018-10-05 by the reprex package (v0.2.1)


#2

solution https://github.com/eheinzen/arsenal/issues/143#issuecomment-427933192

library(arsenal)

 df <-  data.frame(arm = sample(c("Int","Contr"),size=10, replace = T), 
                  sex = sample(c("Male", "Female"),size=10, replace = T),
                 agegp = sample(c("<15","15-49",">50"),size=10, replace = T))

tab1 <- tableby(arm ~ sex + agegp, data=df)

summary(tab1, text=TRUE)
#> 
#> 
#> |          | Contr (N=7) | Int (N=3) | Total (N=10) | p value|
#> |:---------|:-----------:|:---------:|:------------:|-------:|
#> |sex       |             |           |              |   1.000|
#> |-  Female |  3 (42.9%)  | 1 (33.3%) |  4 (40.0%)   |        |
#> |-  Male   |  4 (57.1%)  | 2 (66.7%) |  6 (60.0%)   |        |
#> |agegp     |             |           |              |   0.665|
#> |-  <15    |  1 (14.3%)  | 0 (0.0%)  |  1 (10.0%)   |        |
#> |-  >50    |  5 (71.4%)  | 2 (66.7%) |  7 (70.0%)   |        |
#> |-  15-49  |  1 (14.3%)  | 1 (33.3%) |  2 (20.0%)   |        |

summary(tableby(arm ~ sex + agegp, data = df, cat.stats = "countrowpct"), text = TRUE)
#> 
#> 
#> |          | Contr (N=7) | Int (N=3) | Total (N=10) | p value|
#> |:---------|:-----------:|:---------:|:------------:|-------:|
#> |sex       |             |           |              |   1.000|
#> |-  Female |  3 (75.0%)  | 1 (25.0%) |  4 (100.0%)  |        |
#> |-  Male   |  4 (66.7%)  | 2 (33.3%) |  6 (100.0%)  |        |
#> |agegp     |             |           |              |   0.665|
#> |-  <15    | 1 (100.0%)  | 0 (0.0%)  |  1 (100.0%)  |        |
#> |-  >50    |  5 (71.4%)  | 2 (28.6%) |  7 (100.0%)  |        |
#> |-  15-49  |  1 (50.0%)  | 1 (50.0%) |  2 (100.0%)  |        |

Created on 2018-10-09 by the reprex package (v0.2.1)