Seydou GORO
Seydou GORO

Reputation: 1285

How to calculate percentages by row with function tableby in r

I used the function tableby from the package arsenal to calculate percentages, but percentages are calculated by column by default. What I want is to calculate percentages by row

library(arsenal)

summary(tableby(
  décès~Sexe+`Lieu de vie`+`Nombre de principes actifs sur l'ordonnance`+
    `Clairance de la créatinine`+Immunodepression+IMC+Fièvre+Toux+
    `Perte de poids récente` + `localisation3c` + 
    `Evenement indésirable lié au trt` + resistance, data=analytiqueDC
))

enter image description here

Upvotes: 1

Views: 680

Answers (1)

Ben
Ben

Reputation: 30474

To include percentages by row, add argument cat.stats="countrowpct" to your tableby function.

Here is a complete example:

library(arsenal)

set.seed(100)
nsubj <- 90
mdat <- data.frame(Response=sample(c(1,2,3),nsubj, replace=TRUE),
                   Sex=sample(c("Male", "Female"), nsubj,replace=TRUE),
                   Age=round(rnorm(nsubj,mean=40, sd=5)),
                   HtIn=round(rnorm(nsubj,mean=65,sd=5)))

out <- tableby(Response ~ Sex + Age + HtIn, data=mdat, cat.stats="countrowpct")

labels(out) <- c(Age="Age (years)", HtIn="Height (inches)")
summary(out, stats.labels=c(meansd="Mean-SD", q1q3 = "Q1-Q3"), text=TRUE)

Output

|                |    1 (N=25)     |    2 (N=31)     |    3 (N=34)     |  Total (N=90)   | p value|
|:---------------|:---------------:|:---------------:|:---------------:|:---------------:|-------:|
|Sex             |                 |                 |                 |                 |   0.232|
|-  Female       |   17 (34.0%)    |   14 (28.0%)    |   19 (38.0%)    |   50 (100.0%)   |        |
|-  Male         |    8 (20.0%)    |   17 (42.5%)    |   15 (37.5%)    |   40 (100.0%)   |        |
|Age (years)     |                 |                 |                 |                 |   0.547|
|-  Mean-SD      | 40.200 (4.021)  | 40.161 (3.796)  | 39.265 (3.671)  | 39.833 (3.796)  |        |
|-  Range        | 29.000 - 48.000 | 33.000 - 51.000 | 30.000 - 48.000 | 29.000 - 51.000 |        |
|Height (inches) |                 |                 |                 |                 |   0.093|
|-  Mean-SD      | 63.360 (5.322)  | 66.516 (4.878)  | 65.000 (5.684)  | 65.067 (5.402)  |        |
|-  Range        | 52.000 - 78.000 | 57.000 - 78.000 | 50.000 - 79.000 | 50.000 - 79.000 |        |

Upvotes: 2

Related Questions