Reputation: 655
I have a regression table that I want to export to Latex. But the object I'm working with does not support any traditional package like 'stargazer', or 'texreg'. I have to build it manually. This are the results:
Estimate Std. Error z value
(Intercept):1 -4.652887e-01 3.149742e-01 -1.477228
x1 -3.277175e-05 1.014935e-05 -3.228952
x2 -2.446376e-05 6.717509e-06 -3.641790
x3 1.666235e+00 5.228731e-01 3.186691
x4 2.507294e-08 5.276769e-09 4.751570
What I want is:
(Intercept) -4.652887e-01
(3.149742e-01)
x1 -3.277175e-05
(1.014935e-05)
x2 -2.446376e-05
(6.717509e-06)
x3 1.666235e+00
(5.228731e-01)
x4 2.507294e-08
(5.276769e-09)
I could do this manually (see below), but I was looking for something more dynamic that I could apply in several contexts.
res <- matrix(NA, nrow=2*dim(ctable), ncol=1)
res[1,] <- ctable[1,1]
res[3,] <- ctable[2,1]
res[5,] <- ctable[3,1]
res[7,] <- ctable[4,1]
res[9,] <- ctable[5,1]
res[2,] <- ctable[1,2]
res[4,] <- ctable[2,2]
res[6,] <- ctable[3,2]
res[8,] <- ctable[4,2]
res[10,] <- ctable[5,2]
Thanks!
Upvotes: 1
Views: 1478
Reputation: 48
I dont know if I understood well your problem. But if you want just create your table dynamically, you can use the function below.
create_table = function(table_)
{
p_table = data.frame(Variables=NA, Values=NA)
for(i in 1:dim(table_)[1])
{
est_error = table_[i,1:2] # Im assuming that you know that 2 first columns are the values that you want
name = c(rownames(table_)[i], '') # The final table rownames
name_values = cbind(Variables=name, Values=est_error)
p_table = rbind(p_table, name_values)
}
rownames(p_table) = 1:dim(p_table)[1]
return(na.omit(p_table))
}
And you can use this function in the following way. Below, one example using the mtcars dataset.
ctable = summary(lm(mpg~. , data = mtcars))$coefficients
output_table = create_table(ctable)
The output is something like you posted. Then, is easy create a tex table from a R dataframe using the xtable package.
xtable::xtable(output_table)
Upvotes: 3