The frequency tables are a very basic and important concept in statistics, and we will explain its basic usage here.
First we need to load the data and see the basic information of the data
Install.packages (' vcd ') #安装vcd包, where available data arthritislibrary (VCD) are loaded into the required thread pack:grid> Head (arthritis) ################ ################################ #ID Treatment Sex age Improved1 treated Male Some2 29 treated Male None3 treated Male-None4-treated Male-Marked5-treated Male, Marked6 treated Male Marked> Class (arthritis) ################################################[1] "Data.frame" > Summary ( Arthritis) ############################################# #ID treatment Sex age improved Min. : 1.00 placebo:43 female:59 Min. : 23.00 none:42 1st qu.:21.75 treated:41 male:25 1st qu.:46.00 some:14 median:42.50 median:57.00 marked:28 mean:42.50 mean:53.36 3rd qu.:63.25 3rd qu.:63.00 Max. : 84.00 Max. : 74.00
As seen from the results, arthritis is the data of a data.frame structure. Where ID and age are data of type numeric, the other three are data of type factor.
Create a one-dimensional column table
> a<-table (arthritis$improved) #创建一维列联表 > class (a) #查看变量a的类型 [1] "table" > Anone Some Marked 14
As can be seen from the results, it is the following table
None |
Some |
Marked |
42 |
14 |
28 |
Create a two-dimensional column table
> b<-table (arthritis$sex,arthritis$improved) > class (b) [1] "table" > B None Some Marked Female Male 2 6
The result is the following table
|
None |
Some |
Marked |
Female |
25 |
12 |
22 |
Male |
17 |
2 |
6 |
We can also convert a one-dimensional list of table A and two-dimensional column table B into a percentage form
> prop.table (a) none Some Marked 0.5000000 0.1666667 0.3333333 > prop.table (b) none Some Marked Female 0.29761905 0.14285714 0.26190476 Male 0.20238095 0.02380952 0.07142857
add a marginal to table and
> Addmargins (a) #################################### None Some Marked Sum 28 > Addmargins (b) #################################### None Some Marked sum Female - Male 2 6 sum 84> addmargins (Prop.table (a)) ######################### None Some Marked Sum 0.5000000 0.1666667 0.3333333 1.0000000 > Addmargins (prop.table (b)) ######################### None Some Marked Sum Female 0.29761905 0.14285714 0.26190476 0.70238095 Male 0.20238095 0.02380952 0.07142857 0.29761905 Sum 0.50000000 0.16666667 0.33333333 1.00000000
add only a portion of the marginal and
> Addmargins (prop.table (b), 1) None Some Marked Female 0.29761905 0.14285714 0.26190476 Male 0.20238095 0.02380952 0.07142857 Sum 0.50000000 0.16666667 0.33333333> addmargins (prop.table (b), 2) None Some Marked Sum Female 0.29761905 0.14285714 0.26190476 0.70238095 Male 0.20238095 0.02380952 0.07142857 0.29761905
Reference: R language Combat
Reprint Please specify source: http://blog.csdn.net/zhyoulun/article/details/46433163
[R language Statistics] frequency tables