具有sapply和htest类的Hmisc :: latex产生了许多表

时间:2012-12-03 13:40:24

标签: r latex hmisc

我无法获得Hmisc::latexsapply功能的表格。 latex返回许多表。我想使用Hmisc::latex,因为它更灵活,可配置。

重现问题的步骤:

library(xtable)
library(Hmisc)
library(nortest)
set.seed(1)
x <- matrix(rnorm(10*100), nrow=100)
norm.x <- sapply(x, sf.test)[1:2,]
latex(norm.x, file="", dec=2)
xtable(norm.x) # In contrast xtable produce pretty output

修改

解决方案(感谢Sven Hohenstein):

library(Hmisc)
library(nortest)
set.seed(1)
x <- matrix(rnorm(10*100), nrow=100)
norm.x <- sapply(x, function(z) unlist(sf.test(z)[c("statistic", "p.value")]))
latex(norm.x, file="", dec=2)

1 个答案:

答案 0 :(得分:1)

您可以使用norm.xsapply(x, sf.test)[1:2,]函数修改unlist返回的对象matrix

norm.x <- matrix(unlist(norm.x), nrow = 2, dimnames = list(c("w", "p-value")))

完整的代码:

library("xtable")
library("Hmisc")
library("nortest")
set.seed(1)
x <- data.frame(replicate(10, rnorm(100)))
norm.x <- sapply(x, sf.test)[1:2,]
norm.x <- matrix(unlist(norm.x), nrow = 2, dimnames = list(c("w", "p-value")))
latex(norm.x, file="", dec=2)
xtable(norm.x)