Home > Enterprise >  incorrect calculation of percentages in the frequency table in R
incorrect calculation of percentages in the frequency table in R

Time:02-20

Suppose here mydata

    mydata=structure(list(patient_id = c("01-003", "01-003", "01-003", "01-003", 
"01-003", "01-003", "01-003", "01-004", "01-004", "01-004", "01-004", 
"01-004", "01-004"), group = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), AE = c("increase in lymphocytes", "increase in abs. lymphocytes", 
"increase in lymphocytes", "decrease in abs. neutrophils", "decrease in neutrophils", 
"decrease in abs. Monocytes", "decrease in monocytes", "increase in lymphocytes", 
"increase in abs. lymphocytes", "increase in lymphocytes", "decrease in abs. neutrophils", 
"decrease in neutrophils", "decrease in abs. Monocytes"), link = c("Connected", 
"Connected", "Connected", "Connected", "Connected", "Connected", 
"Not connected", "Connected", "Connected", "Not connected", "Connected", 
"Not connected", "Connected")), class = "data.frame", row.names = c(NA, 
-13L))

for example, patients with id 003 and 004 have the same adverse event, which is an increase in lymphocytes. I perform frequency table

library(table1)
table1(~AE|link  group, data = mydata)

and got the result enter image description here

In my reproducible example only two pacients, but here N=10. This means that it simply counted the total number of ocurredae(10) and then, count particular ae which occured 2 times divided on 10 (2/10=20%). but count of particular ae should be divided by the number of patients with this ae. In this case, it ae occured in two man and the result should be like this 2(100%). 2ae / 2pacients =1(or 100%) and N must be equal not total number of all ae in sampe, but count of pacients in sample. Here only two man.

How can I take this condition into account to get the correct frequency table? Thank you very much for your help

CodePudding user response:

It seems you have misspecified the table1 model. Reverse the order as:

table1(~ link group | AE, data = mydata)

  • Related