我使用 expss 包在 R 中定义了一些表函数来自动制表。我的一张表想要显示类别的案例或百分比,然后是平均值。平均值可以基于相同的类别变量,也可以定义为另一个变量。总的来说,代码完美无缺。对于某些变量,尽管我一直在努力解决错误代码“do.call(data.frame, c(x, alis)) 中的错误:变量名称限制为 10000 字节”
此表的代码
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)
}
))
}
这段代码总体上运行完美。
Table2(Q8_cat)
对于某些变量,尽管它会生成错误代码
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))
同时在代码中包含变量再次完美
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?")
}
有什么建议吗?或者任何人知道为什么会发生错误?
谢谢