This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

Example 1

Premed=c(13,19,90)#frequencie in first row 
Others=c(8,11,359)#frequencies in second row
college.table=rbind(Premed, Others)#make a table
print(college.table) #not necessary  
##        [,1] [,2] [,3]
## Premed   13   19   90
## Others    8   11  359
test_Example1=chisq.test(college.table) #the chisquare test

test_Example1
## 
##  Pearson's Chi-squared test
## 
## data:  college.table
## X-squared = 45.283, df = 2, p-value = 1.469e-10
test_Example1$expected
##          [,1]  [,2]    [,3]
## Premed  5.124  7.32 109.556
## Others 15.876 22.68 339.444

Example 2

prof_Bus=c(55, 38, 7)
skilled= c(79,71,25)
unskilled= c(22, 75, 3)
farmer=c(15,23,10,32)

career.table=rbind(prof_Bus, skilled, unskilled, farmer)#make a table
## Warning in rbind(prof_Bus, skilled, unskilled, farmer): number of columns of
## result is not a multiple of vector length (arg 1)
print(career.table) #not necessary  
##           [,1] [,2] [,3] [,4]
## prof_Bus    55   38    7   55
## skilled     79   71   25   79
## unskilled   22   75    3   22
## farmer      15   23   10   32
test_Example2=chisq.test(career.table) #the chisquare test

test_Example2
## 
##  Pearson's Chi-squared test
## 
## data:  career.table
## X-squared = 65.893, df = 9, p-value = 9.667e-11
test_Example2$expected
##               [,1]     [,2]     [,3]     [,4]
## prof_Bus  43.37971 52.51227 11.41571 47.69231
## skilled   71.08674 86.05237 18.70704 78.15385
## unskilled 34.14403 41.33224  8.98527 37.53846
## farmer    22.38953 27.10311  5.89198 24.61538

Example 3

centerA=c(50, 26,24)
centerB=c(40,34,26)
centerC=c(40, 20, 40)

center.table=rbind(centerA, centerB, centerC)#make a table
print(center.table) #not necessary  
##         [,1] [,2] [,3]
## centerA   50   26   24
## centerB   40   34   26
## centerC   40   20   40
test_Example2=chisq.test(center.table) #the chisquare test

test_Example2
## 
##  Pearson's Chi-squared test
## 
## data:  center.table
## X-squared = 10.305, df = 4, p-value = 0.03559
test_Example2$expected
##             [,1]     [,2] [,3]
## centerA 43.33333 26.66667   30
## centerB 43.33333 26.66667   30
## centerC 43.33333 26.66667   30

Example 4

Lab3_data=read.csv("Lab3Data.csv")

# make two way table
a=table(Lab3_data$HUNVFlag,Lab3_data$Urban)
rownames(a)=c("Sufficient access", "Not sufficient access")
colnames(a)=c("Non-urban", "Urban")
a
##                        
##                         Non-urban Urban
##   Sufficient access           410   707
##   Not sufficient access        54   336
b=prop.table(a,2)
barplot(b, beside=TRUE, legend=T, col=c("green","red"), main="Food access in urban and non-urban setting") 

test_Example4=chisq.test(a) #the chisquare test
test_Example4
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  a
## X-squared = 69.816, df = 1, p-value < 2.2e-16
test_Example4$expected
##                        
##                         Non-urban    Urban
##   Sufficient access      343.9204 773.0796
##   Not sufficient access  120.0796 269.9204

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.