[R] Help with data frame conversion to contingency table

Rui Barradas ruipbarradas at sapo.pt
Wed Aug 28 23:42:32 CEST 2013


Hello,

If you want to perform a chi suqare test, you don't need to make a 
contingency table. From the help page for ?chisq.test, section Details:

"If x is a matrix with at least two rows and columns, it is taken as a 
two-dimensional contingency table"

So all you need is to pass a two column matrix or data.frame:

dat <- read.table(text = "
Facial.Type          North.Indians 	South.Indians
1      Leptoprosopic            96           115
2 Hyperleptoprosopic       189           132
3       Mesoprosopic            68           100
4       Euryprosopic            	38            34
5  Hypereuryprosopic            9            19
", header = TRUE)

chisq.test(dat[-1])

# or, equivalently
chisq.test(dat[, 2:3])


Hope this helps,

Rui Barradas

Em 28-08-2013 17:10, SHASHWAT MISHRA escreveu:
> Hi,
>
> I am a beginner at R and do not have a strong background in statistics. I hope somebody can help me with this. I have a data frame which looks like this
>   Facial.Type          North.Indians 	South.Indians
> 1      Leptoprosopic            96           115
> 2 Hyperleptoprosopic       189           132
> 3       Mesoprosopic            68           100
> 4       Euryprosopic            	38            34
> 5  Hypereuryprosopic            9            19
>
> Now I want to make a contingency table (5 x 2) with this data and apply the chi square test. How can it be done?
> A step by step instruction would be greatly appreciated
>
> Thanks
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list