[R] Contingency table in R

Jim Lemon jim at bitwrit.com.au
Thu Mar 3 10:06:22 CET 2011


On 03/03/2011 01:13 AM, Laura Clasemann wrote:
>
> Hi,
>
> I have a table in R with data I needed and need to create a contingency table out of it. The table I have so far looks like this:
>
>
>                     Binger
> r
> DietType     No Yes
>    Dangerous  15  12
>    Healthy    52   9
>    None      134  24
>    Unhealthy  72  23
>
> These are the error messages that I keep getting whenever I try to get a contingency table. I'm not sure why it won't work for me, any help would be appreciated!
>> nametable<-table(excat,recat)
> Error in table(excat, recat) : object 'excat' not found
>   		 	   		
Hi Laura,
The above looks like a contingency table, but I suspect that it is in a 
format that is not recognized as such by R. If I read in the above, less 
the top two lines ("Binger" and "r"), I get a data frame.

lc.df<-read.table("lc.dat",header=TRUE)
lc.df
    DietType  No Yes
1 Dangerous  15  12
2   Healthy  52   9
3      None 134  24
4 Unhealthy  72  23

If I then try to run a chi-square test on the numeric columns of the 
data frame,

chisq.test(lc.df[,2:3])

         Pearson's Chi-squared test

data:  lc.df[, 2:3]
X-squared = 14.5011, df = 3, p-value = 0.002297

I get the expected result. If the table in your message is something 
like a table in a word processing document or a text file, R doesn't 
know what it is. If it is indeed an R object (which I doubt) it probably 
isn't named "excat" (or "recat" for that matter). That may be what is 
causing the error message.

A final point is that you probably want your table arranged in order of 
the presumed healthiness of the diet, i.e.

Healthy
None
Unhealthy
Dangerous

because I think you are trying to discover whether bingers are more 
likely to report less healthy diets.

Jim



More information about the R-help mailing list