Reputation: 25
I am still very new to R (Version 1.1.383 on Mac), so I apologize if this question is basic. I have searched for a solution on the internet and on this page but could not find any.
What I want to do is to make a proportion table on which I can use chisq.test()
to test if there is a difference between 4 patient groups in the distribution of proportion of patients with 5 different conditions.
The data I have contains percentage of patients with conditions (CHF, DM, S, MI and CHF) and the patients groups (green, yellow, orange and red). There are 238, 196, 158 and 20 patients in the 4 groups, respectively, and some patients may have more than one condition. Therefore, I use proportions and not actual number of patients.
Like this:
condition <- c("CHF", "DM", "S", "MI", "CHF")
green <- c(30,33,35,17,15)/238*100
yellow <- c(47,32,25,21,19)/196*100
orange <- c(48,24,27,27,25)/158*100
red <- c(10,2,4,1,6)/20*100
Where 30, 47, 48 and 10 patients in the four groups have CHF. 33, 32, 24 and 2 patients have DM and so on...
Using cbind()
I get this matrix:
df <- cbind(condition, green, yellow, orange, red )
df
condition green yellow orange red #I only included 1 decimal.
[1,] "CHF" "12.6" "24.9" "30.4" "50"
[2,] "DM" "13.9" "16.3" "15.2" "10"
[3,] "S" "14.7" "12.8" "17.1" "20"
[4,] "MI" "7.1" "10.7" "17.1" "5"
[5,] "CHF" "6.3" "9.7" "15.8" "30"
It almost looks like what I want but "" makes me suspect that it is coded as characters and not a proper proportion table. Also this happens when I use chisq.test()
:
chisq.test(df)
Error in sum(x) : invalid 'type' (character) of argument
I really hope you can help. How do I make a proper proportion table and test difference in distribution of conditions in the four groups?
Upvotes: 2
Views: 633
Reputation: 887871
We created a matrix with cbind
and matrix can have only single class. Having 'condition' as a column changes the numeric columns to character
and this creates the issue in chisq.test
.
According to ?chisq.test
, the input argument 'x' can be vector
or matrix
, but the permitted class is numeric
or factor
.
x - a numeric vector or matrix. x and y can also both be factors.
So, create the matrix
with only numeric
vectors and keep the 'condition' as row names.
df <- cbind(green, yellow, orange, red)
row.names(df) <- condition
chisq.test(df)
Upvotes: 2