Question

I can't get table with Hmisc::latex and sapply function. latex return many tables. I would like to use a Hmisc::latex because it more flexible configurable.

Steps to reproduce problem:

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

Edit:

Solution (thanks to 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)
Was it helpful?

Solution

You could modify the object norm.x returned by sapply(x, sf.test)[1:2,] with the functions unlist and matrix.

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

The complete code:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top