如何让addNA和xtable一起工作?
我正在使用 xtabs
来对一些包含 NA
的数据进行制表。为了确保总数完整,我使用 addNA
来计算缺少因子水平的数量。
然而,当使用xtable
导出到LaTeX进行Sweaving时,这会导致问题,因为现在行和列名称中有NA
。我有一个解决方案:
rownames(tab)[is.na(rownames(tab))]<-"NA"
colnames(tab)[is.na(colnames(tab))]<-"NA"
但这对于很多表来说可能会变得令人厌烦,有没有一种方法可以更自动地执行此操作?或者是否有更好的方法来生成表格?
I'm using xtabs
to tabulate some data that contains NA
s. In order to make sure the totals are complete, I'm using addNA
to count the ones with missing factor levels.
However, this causes problems when using xtable
to export to LaTeX for Sweaving because there are NA
s in the row and column names now. I have a solution:
rownames(tab)[is.na(rownames(tab))]<-"NA"
colnames(tab)[is.na(colnames(tab))]<-"NA"
But this can become tiresome for lots of tables, is there a way of doing this more automatically? Or is there a better way of producing the tables in the first place?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的问题。我也找不到使用 xtable 本身来处理这个问题的方法。因此,我最好的建议是将您的解决方法变成一个可以轻松调用的小函数。
例如:
这会创建有效的乳胶而不会出现错误:
Interesting question. I couldn't find a way of dealing with this using xtable itself, either. So the best I can suggest is to turn your workaround into a little function that can then be called easily.
For example:
This creates valid latex without error:
另一个需要考虑的解决方案是使用修改后的 addNA 来允许它首先将因子级别作为字符串输出:
Another solution to consider is to use a modified
addNA
to allow it to output the factor level as a string in the first place: