[R] contingency tables in R
Jin Choi
oohpsjin at gmail.com
Sun Jul 22 18:32:23 CEST 2012
Hello!
I am interested in creating contingency tables, namely one that would
let me find the frequency and proportion of patients with specific
risk factors (dyslipidemia, diabetes, obesity, smoking, hypertension).
There are 3 dimensions I would like to divide the population into:
sex, family history, and number of risk factors.
In R, I used the following code:
mytable<-xtabs(~sex+familyhist+numrisk+hypertension,data=mydata)
ftable(mytable)
a<-ftable(mytable)
prop.table(a,1)
However, when I conduct the following code:
mytable<-xtabs(~sex+familyhist+numrisk+hypertension+diabetes+obesity+smoking+dyslipidemia,data=mydata)
Here the table simply considers the additional risk factors as new
dimensions, which I do not want. I would like to find a way where the
dimensions are sex, family history, and number of risk factors and I
am finding the frequency and prevalence for each risk factor
(dyslipidemia, diabetes, obesity, smoking, hypertension) in each of
these subgroups.
The only way to get around this problem I could think of is to create
new data frames for each number of risk factor subgroup: numrisk1,
numrisk2, numrisk3…where numrisk1 indicates population with 1 risk
factor. Then I could calculate the prevalence of each risk factor
separately. This approach will take a very long time so I was hoping
to ask if anyone knew of a solution to this issue I am having with
contingency tables...perhaps a useful R package?
Thank you for your help!
Jin Choi
Masters Student (Epidemiology)
McGill University
More information about the R-help
mailing list