r table

一、table 函数对应的就是统计学中的列联表,是一种记录频数的方法,对于统计来说有非常重要的应用,下面的例子都是针对维数为2的情况举例,多维的情况是类似的

下面看一个例子:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. > ct <- data.frame(  
  2. +         Vote.for.X = factor(c("Yes", "Yes", "No", "Not Sure", "No"), levels = c("Yes", "No", "Not Sure")),  
  3. +         Vote.for.X.Last.Time =  factor(c("Yes", "No", "No", "Yes", "No"), levels = c("Yes", "No"))  
  4. +       )  
  5. > ct  
  6.   Vote.for.X Vote.for.X.Last.Time  
  7. 1        Yes                  Yes  
  8. 2        Yes                   No  
  9. 3         No                   No  
  10. 4   Not Sure                  Yes  
  11. 5         No                   No  
  12. > cttab <-table(ct)  
  13. > cttab  
  14.           Vote.for.X.Last.Time  
  15. Vote.for.X Yes No  
  16.   Yes        1  1  
  17.   No         0  2  
  18.   Not Sure   1  0  


首先我们创建了一个示例数据集合,其中我们指定我们的因子的水平,然后 table 函数则是统计所有因子对出现的情况的频数

下面看一下 cttab 的特点:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. > mode(cttab)  
  2. [1] "numeric"  
  3. > str(cttab)  
  4.  'table' int [1:3, 1:2] 1 0 1 1 2 0  
  5.  - attr(*, "dimnames")=List of 2  
  6.   ..$ Vote.for.X          : chr [1:3] "Yes" "No" "Not Sure"  
  7.   ..$ Vote.for.X.Last.Time: chr [1:2] "Yes" "No"  
  8. > summary(cttab)  
  9. Number of cases in table: 5   
  10. Number of factors: 2   
  11. Test for independence of all factors:  
  12.     Chisq = 2.9167, df = 2, p-value = 0.2326  
  13.     Chi-squared approximation may be incorrect  
  14. > attributes(cttab)  
  15. $dim  
  16. [1] 3 2  
  17.   
  18. $dimnames  
  19. $dimnames$Vote.for.X  
  20. [1] "Yes"      "No"       "Not Sure"  
  21.   
  22. $dimnames$Vote.for.X.Last.Time  
  23. [1] "Yes" "No"   
  24.   
  25.   
  26. $class  
  27. [1] "table"  



二、table对象的操作

一个必须要掌握的操作,addmargins

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. > addmargins(cttab)  
  2.           Vote.for.X.Last.Time  
  3. Vote.for.X Yes No Sum  
  4.   Yes        1  1   2  
  5.   No         0  2   2  
  6.   Not Sure   1  0   1  
  7.   Sum        2  3   5  


下面取出各维度的名字,也就是各个的水平

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. > dimnames(cttab)  
  2. $Vote.for.X  
  3. [1] "Yes"      "No"       "Not Sure"  
  4.   
  5. $Vote.for.X.Last.Time  
  6. [1] "Yes" "No"   


下面提取感兴趣的子表:subtable 类比 subset

subtable(tbl,subnames) tbl 感兴趣的表,subnames 一个类表,列出自己各个维度感兴趣的水平, subtable 实现如下

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. subtable <- function(tbl, subnames) {  
  2.   #将 table 转换称 array 获得 table 里面的所有元素  
  3.   tblarray <- unclass(tbl)  
  4.     
  5.   #将 tblarray 以及 subnames 组合到一个list中  
  6.   dcargs <- list(tblarray)  
  7.   ndims <- length(subnames)  
  8.   for(i in 1:ndims) {  
  9.     dcargs[[i+1]] <- subnames[[i]]  
  10.   }  
  11.     
  12.   #等价与执行 dcargs[[1]][dcargs[[2]][i], dcargs[[3]][j]] i,j 取遍所有该属性的元素  
  13.   subarray <- do.call("[", dcargs)  
  14.    
  15.   #对list中的每一个属性调用 length  
  16.   dims <- lapply(subnames, length)  
  17.   subtbl <- array(subarray, dims, dimnames = subnames)  
  18.   class(subtbl) <- "table"  
  19.   return(subtbl)  
  20. }   


下面给出一个例子:可能很有用的

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. > as.data.frame(cttab)  
  2.   Vote.for.X Vote.for.X.Last.Time Freq  
  3. 1        Yes                  Yes    1  
  4. 2         No                  Yes    0  
  5. 3   Not Sure                  Yes    1  
  6. 4        Yes                   No    1  
  7. 5         No                   No    2  
  8. 6   Not Sure                   No    0  

tabdom 计算table的统计频率

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. tabdom <- function(tbl, k) {  
  2.   tbldf <- as.data.frame(tbl)  
  3.   freqord <- order(tabldf$Freq, decreasing=TRUE)  
  4.   dom <- tbldf[freqord, ][1:k]  
  5.   return(dom)  
  6. }  



注意:aggregate() 函数  cut() 函数

原文地址:https://www.cnblogs.com/awishfullyway/p/6633713.html