Re: [R] Contingency table in R

From: Jim Lemon <jim_at_bitwrit.com.au>
Date: Thu, 03 Mar 2011 20:06:22 +1100

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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 03 Mar 2011 - 09:10:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 03 Mar 2011 - 13:50:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive