[R语言统计]频数表
频数表在统计学中是一个非常基本并且重要的概念,我们这里就来讲解它的基本用法。
首先我们需要载入数据,并查看数据的基本信息
- install.packages('vcd') #安装vcd包,其中有可以利用的数据Arthritis
- library(vcd)
- 载入需要的程辑包:grid
- > head(Arthritis)#################################################
- ID Treatment Sex Age Improved
- 1 57 Treated Male 27 Some
- 2 46 Treated Male 29 None
- 3 77 Treated Male 30 None
- 4 17 Treated Male 32 Marked
- 5 36 Treated Male 46 Marked
- 6 23 Treated Male 58 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
从结果中看以看出,Arthritis是一个data.frame结构的数据。其中ID和Age是numeric型的数据,其他三个都是factor型的数据。
创建一维列联表
- > a<-table(Arthritis$Improved)#创建一维列联表
- > class(a) #查看变量a的类型
- [1] "table"
- > a
- None Some Marked
- 42 14 28
从结果中可以看出,就是如下的表格
None | Some | Marked |
42 | 14 | 28 |
创建二维列联表
- > b<-table(Arthritis$Sex,Arthritis$Improved)
- > class(b)
- [1] "table"
- > b
- None Some Marked
- Female 25 12 22
- Male 17 2 6
结果是如下表格
None | Some | Marked | |
Female | 25 | 12 | 22 |
Male | 17 | 2 | 6 |
我们还可以将一维列联表a和二维列联表b转化成百分比的形式
- > 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
给table添加边际和
- > addmargins(a)####################################
- None Some Marked Sum
- 42 14 28 84
- > addmargins(b)####################################
- None Some Marked Sum
- Female 25 12 22 59
- Male 17 2 6 25
- Sum 42 14 28 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
只添加某个部分边际和
- > 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