• [R语言统计]频数表


    频数表在统计学中是一个非常基本并且重要的概念,我们这里就来讲解它的基本用法。

    首先我们需要载入数据,并查看数据的基本信息

    [python] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. install.packages('vcd') #安装vcd包,其中有可以利用的数据Arthritis  
    2. library(vcd)  
    3.   
    4. 载入需要的程辑包:grid  
    5. > head(Arthritis)#################################################  
    6. ID Treatment  Sex Age Improved  
    7. 57   Treated Male  27     Some  
    8. 46   Treated Male  29     None  
    9. 77   Treated Male  30     None  
    10. 17   Treated Male  32   Marked  
    11. 36   Treated Male  46   Marked  
    12. 23   Treated Male  58   Marked  
    13. class(Arthritis)################################################  
    14. [1] "data.frame"  
    15. > summary(Arthritis)##############################################  
    16. ID          Treatment      Sex          Age          Improved   
    17. Min.   : 1.00   Placebo:43   Female:59   Min.   :23.00   None  :42    
    18. 1st Qu.:21.75   Treated:41   Male  :25   1st Qu.:46.00   Some  :14    
    19. Median :42.50                            Median :57.00   Marked:28    
    20. Mean   :42.50                            Mean   :53.36                
    21. 3rd Qu.:63.25                            3rd Qu.:63.00                
    22. Max.   :84.00                            Max.   :74.00  

    从结果中看以看出,Arthritis是一个data.frame结构的数据。其中ID和Age是numeric型的数据,其他三个都是factor型的数据。

    创建一维列联表

    [python] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. > a<-table(Arthritis$Improved)#创建一维列联表  
    2. class(a)                    #查看变量a的类型  
    3. [1] "table"  
    4. > a  
    5.   
    6. None   Some Marked   
    7. 42     14     28   


    从结果中可以看出,就是如下的表格

    None Some Marked
    42 14 28

    创建二维列联表

    [python] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. > b<-table(Arthritis$Sex,Arthritis$Improved)  
    2. class(b)  
    3. [1] "table"  
    4. > b  
    5.           
    6.          None Some Marked  
    7.   Female   25   12     22  
    8.   Male     17    2      6  


    结果是如下表格

      None Some Marked
    Female 25 12 22
    Male 17 2 6

     我们还可以将一维列联表a和二维列联表b转化成百分比的形式

    [python] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. > prop.table(a)  
    2.   
    3.      None      Some    Marked   
    4. 0.5000000 0.1666667 0.3333333   
    5. > prop.table(b)  
    6.           
    7.                None       Some     Marked  
    8.   Female 0.29761905 0.14285714 0.26190476  
    9.   Male   0.20238095 0.02380952 0.07142857  


    给table添加边际和

    [python] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. > addmargins(a)####################################  
    2.   
    3.   None   Some Marked    Sum   
    4.     42     14     28     84   
    5. > addmargins(b)####################################  
    6.           
    7.          None Some Marked Sum  
    8.   Female   25   12     22  59  
    9.   Male     17    2      6  25  
    10.   Sum      42   14     28  84  
    11. > addmargins(prop.table(a))#########################  
    12.   
    13.      None      Some    Marked       Sum   
    14. 0.5000000 0.1666667 0.3333333 1.0000000   
    15. > addmargins(prop.table(b))#########################  
    16.           
    17.                None       Some     Marked        Sum  
    18.   Female 0.29761905 0.14285714 0.26190476 0.70238095  
    19.   Male   0.20238095 0.02380952 0.07142857 0.29761905  
    20.   Sum    0.50000000 0.16666667 0.33333333 1.00000000  


    只添加某个部分边际和

    [python] view plain copy
     
     在CODE上查看代码片派生到我的代码片
      1. > addmargins(prop.table(b),1)  
      2.           
      3.                None       Some     Marked  
      4.   Female 0.29761905 0.14285714 0.26190476  
      5.   Male   0.20238095 0.02380952 0.07142857  
      6.   Sum    0.50000000 0.16666667 0.33333333  
      7. > addmargins(prop.table(b),2)  
      8.           
      9.                None       Some     Marked        Sum  
      10.   Female 0.29761905 0.14285714 0.26190476 0.70238095  
      11.   Male   0.20238095 0.02380952 0.07142857 0.29761905  
  • 相关阅读:
    SQL注入(转载)
    htmlTable To jpeg
    oracle 中文乱码 全变成 '靠'
    oracle 跨库查询
    oracle 备份还原数据库
    jquery 非异步读取数据
    oracle 游标 简单使用
    自增列的一种方法收藏
    oracle 导出txt 数据,excel 数据
    oracle 简单job
  • 原文地址:https://www.cnblogs.com/awishfullyway/p/6485214.html
Copyright © 2020-2023  润新知