简体   繁体   English

R: expss package: do.call(data.frame, c(x, alis)) 错误:变量名称限制为 10000 字节

[英]R: expss package: Error in do.call(data.frame, c(x, alis)) : variable names are limited to 10000 bytes

I defined some table functions in R using the expss package to automate tabulation.我在 R 中定义了一些表函数,使用 expss package 来自动制表。 One of my tables wants to show cases or percentages on categories followed by the mean.我的一张表想要显示类别的个案或百分比,后跟平均值。 The mean can be based on the same category variable or it can be defined to be another variable.平均值可以基于相同的类别变量,也可以定义为另一个变量。 Overall the code works perfect.总的来说,代码工作完美。 For some variables though I keep struggling with the error code "Error in do.call(data.frame, c(x, alis)): variable names are limited to 10000 bytes"对于某些变量,尽管我一直在努力解决错误代码“do.call(data.frame, c(x, alis)) 中的错误:变量名称限制为 10000 字节”

the code for this table该表的代码

  Table2 = function (Q, banner=banner, caption , Q.mean, ddata=d, questlab=dquest, mis.val=999) {
  x_totaln<-eval(substitute(x),ddata)
  x_totaln[is.na(eval(substitute(Q),ddata))]<-NA
  if(missing(Q.mean))
  {Q_mean<-eval(substitute(Q),ddata)}
  else 
  {Q_mean<-eval(substitute(Q.mean),ddata)}
  Q_mean[Q_mean==mis.val]<-NA
  if(missing(caption))
  {caption<-eval(substitute(var_lab(Q_mean)),questlab)}
  eval.parent(substitute(
    { 
      banner %>%
        tab_cells (x_totaln) %>%
        tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
        tab_cells (Q) %>%
        tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
        tab_stat_cpct(total_row_position = c("none"), label = '%') %>%
        tab_cells (Q_mean) %>%
        tab_stat_mean(label = 'Mean') %>%
        tab_pivot (stat_position = "inside_rows") %>%  
        drop_c ()  %>%
        custom_format2()  %>%
        set_caption(caption)
    }
  ))
}

This code is overall working perfect.此代码总体上工作完美。

Table2(Q8_cat)

在此处输入图像描述

For some variables though it generates the error code对于某些变量,虽然它会生成错误代码

Table2(age_cat,Q.mean=age,caption="Your age at the start of the programme?")
 Error in do.call(data.frame, c(x, alis)) : 
  variable names are limited to 10000 bytes 
19.
do.call(data.frame, c(x, alis)) 

while including the variables in the code works again perfect同时在代码中包含变量再次完美

Table2test = function () {
  x_totaln<-eval(substitute(x),d)
  x_totaln[is.na(eval(substitute(age_cat),d))]<-NA
  Q_mean<-eval(substitute(age),d)
  Q_mean[Q_mean==999]<-NA
      banner %>%
        tab_cells (x_totaln) %>%
        tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
        tab_cells (age_cat) %>%
        tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
        tab_stat_cpct(total_row_position = c("none"), label = '%') %>%
        tab_cells (Q_mean) %>%
        tab_stat_mean(label = 'Mean') %>%
        tab_pivot (stat_position = "inside_rows") %>%  
        drop_c ()  %>%
        custom_format2()  %>%
        set_caption("Your age at the start of the programme?")
  
}

在此处输入图像描述

Any advice?有什么建议吗? Or anyone any idea why the error occurs?或者有人知道为什么会发生错误吗?

Thanks谢谢

When you substitute variables in some cases they are represented as structure.当您在某些情况下substitute变量时,它们被表示为结构。 In this case there is no variable name in the expression but only value: tab_cells(structure(c(22, 23, 22, 23, ... many numbers))).在这种情况下,表达式中没有变量名,只有值:tab_cells(structure(c(22, 23, 22, 23, ... many numbers)))。 And we try to use this long representation as name in the resulted table.我们尝试使用这个长表示形式作为结果表中的名称。 But R has limitation on the length of the names.但是 R 对名称的长度有限制。 And here the function fails.而这里的 function 失败了。 Solution is quite simple - we will always set variable labels which we will use as names.解决方案非常简单 - 我们将始终设置我们将用作名称的变量标签。 So the following code run without any errors:所以下面的代码运行没有任何错误:

Table2 = function (Q, banner=banner, caption , Q.mean, ddata=d, questlab=dquest, mis.val=999) {
    x_totaln<-eval(substitute(x),ddata)
    x_totaln[is.na(eval(substitute(Q),ddata))]<-NA
    var_lab(x_totaln) = "Total" # add label for total
    if(missing(Q.mean))
    {Q_mean<-eval(substitute(Q),ddata)}
    else 
    {Q_mean<-eval(substitute(Q.mean),ddata)}
    Q_mean[Q_mean==mis.val]<-NA
    if(missing(caption))
    {caption<-eval(substitute(var_lab(Q_mean)),questlab)}
    eval.parent(substitute(
        { 
            banner %>%
                tab_cells (x_totaln) %>%
                tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
                tab_cells (Q) %>%
                tab_stat_cases(total_row_position = c("none"),label = 'N') %>%
                tab_stat_cpct(total_row_position = c("none"), label = '%') %>%
                tab_cells ("|" = Q_mean) %>% # "|" suppress label for mean
                tab_stat_mean(label = 'Mean') %>%
                tab_pivot (stat_position = "inside_rows") %>%  
                drop_c ()  %>%
                custom_format2()  %>%
                set_caption(caption)
        }
    ))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM