【问题标题】:Edit a function regarding organizing tables编辑有关组织表格的功能
【发布时间】:2017-07-25 01:37:29
【问题描述】:

我有一个看起来像这样的函数:

organize.table <- function(x,y = NULL, col = NULL, row = NULL, sort = NULL, sort.row = sort,
                       sort.col = sort, total = TRUE, other = TRUE, ...){

  if((!is.factor(x) & !is.character(x)) | (!is.factor(y) & !is.character(y))){
    stop("Either x or y is not a factor", call. = FALSE)
  }
 if(length(x) != length(y)){
    stop("Length of x not equal to y", call. = FALSE)
 }
 if(!(sort.row %in% c('desc', 'asc')) | is.null(sort.row) |
    !(sort.col %in% c('desc', 'asc')) |  is.null(sort.col)){
    stop("sort.row or sort.col not equal to NULL, 'desc', or 'asc'")
 }
 if((!is.null(sort.row) | !is.null(sort.col)) & total == FALSE){
   stop("To use sort.row or sort.col, parameter 'total' must equal TRUE", call. = FALSE)
 }
 if(length(unique(y)) <= col | length(unique(x)) <= row){
   stop("Error: Dimension mismatch between x and col or y and row. col < the unique number of 
     attributes in y or row < the unique number of attributes in x", call. = FALSE)
 }
 tabs <- table(x,y, ...)
 cs   <- ncol(tabs)
 rs   <- nrow(tabs)
 if(total == TRUE){
   tabs           <- cbind(tabs, rowSums(tabs))
   colname.tabs   <- c(colnames(tabs)[1:cs], 'Total')
   colnames(tabs) <- colname.tabs
   tabs           <- rbind(tabs, colSums(tabs))
   rowname.tabs   <- c(rownames(tabs)[1:rs], 'Total')
   rownames(tabs) <- rowname.tabs
 }
if(sort.row == 'desc') {
   tabs <- tabs[order(-tabs$Total), ]
   tabs <- tabs[rowname.tabs,]
 }
if(sort.row == 'asc')  {
   tabs <- tabs[order( tabs$Total), ]
   tabs <- rowname.tabs
 }
if(sort.col == 'desc') {
   tabs <- tabs[ ,order(-tabs['Total', ])]
   tabs <- colname.tabs
 }
 if(sort.col == 'asc') {
   tabs <- tabs[ ,order( tabs['Total', ])]
   tabs <- colname.tabs
 }
 if(other == TRUE){
   ra   <- row + 1; ca <- col + 1
    tabs <- cbind(tabs[ ,1:col], rowSums(tabs[ ,ca:cs]), tabs$Total)
    tabs <- rbind(tabs[1:row, ], colSums(tabs[ra:rs, ]), tabs['Total',])
    rownames(tabs) <- c(rownames(tabs)[1:row], 'Other', 'Total')
    colnames(tabs) <- c(colnames(tabs)[1:col], 'Other', 'Total')
  }
}

在工作时,我经常需要生成一个双向表,该表需要该表中的顶部 ncolnrow 值,并将表的其余部分总结为 其他。另外,我通常需要一个 Total 列和行。有时我需要最少(最少)的 ncolnrow 值。这实际上是一个灵活的表函数。我收到错误,即:

tabs$Total 中的错误:$ 运算符对原子向量无效

有关解决此问题的任何建议或代码?

【问题讨论】:

    标签: r


    【解决方案1】:

    在两个if 语句中

    if(sort.col == 'desc') {
       tabs <- tabs[ ,order(-tabs['Total', ])]
       tabs <- colname.tabs
    }
    if(sort.col == 'asc') {
       tabs <- tabs[ ,order( tabs['Total', ])]
       tabs <- colname.tabs
    }
    

    你设置了tabs &lt;- colname.tabs。一旦你这样做了,标签只是一个向量,
    当您尝试使用tabs$Total 时,不是data.frame,因此在后续行中, 你得到这个错误。我认为不是tabs &lt;- colname.tabs 你真正想要的colnames(tabs) &lt;- colname.tabs

    我看到您还有一个 if 声明,其中包括 tabs &lt;- rowname.tabs。那应该是 rownames(tabs) &lt;- rowname.tabs

    重现错误

    您的功能太复杂,我无法从您的设备中重现错误 函数,但我可以在一个简单的示例中得到相同的错误消息。

    colname.tabs   <- c(colnames(iris)[1:4], 'Total')
    colname.tabs 
    [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Total"
    
    colname.tabs$Total
    Error in colname.tabs$Total : $ operator is invalid for atomic vectors
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多