stargazer: error 'names' attribute must be the same length as vector

Viewed 66

I want to use stargazer to export a table as .html:

iso <- c("AUT", "AUT", "BEG", "BEG", "BEG")
year <- c(2005, 2006, 2003, 2006, 2007)
tab1 <- table(iso, year)
stargazer(tab1, type = "html", summary = FALSE, out = "table1.html")

this gives me the following error:

Error in names(x) <- value : 'names' attribute [4] must be the same length as the vector [3]

I don't understand what this means, is it because I have 4 distinct years? How can I get the code to work?

1 Answers

stargazer does not accept table objects:

one or more model objects (for regression analysis tables) or data frames/vectors/matrices (for summary statistics, or direct output of content). They can also be included as lists (or even lists within lists).

What you can do is use as.data.frame for you input:

library(stargazer)
stargazer(as.data.frame(tab1), type = "html", summary = FALSE, out = "table1.html")

Output:

<table style="text-align:center"><tr><td colspan="4" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"></td><td>iso</td><td>year</td><td>Freq</td></tr>
<tr><td colspan="4" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">1</td><td>AUT</td><td>2003</td><td>0</td></tr>
<tr><td style="text-align:left">2</td><td>BEG</td><td>2003</td><td>1</td></tr>
<tr><td style="text-align:left">3</td><td>AUT</td><td>2005</td><td>1</td></tr>
<tr><td style="text-align:left">4</td><td>BEG</td><td>2005</td><td>0</td></tr>
<tr><td style="text-align:left">5</td><td>AUT</td><td>2006</td><td>1</td></tr>
<tr><td style="text-align:left">6</td><td>BEG</td><td>2006</td><td>1</td></tr>
<tr><td style="text-align:left">7</td><td>AUT</td><td>2007</td><td>0</td></tr>
<tr><td style="text-align:left">8</td><td>BEG</td><td>2007</td><td>1</td></tr>
<tr><td colspan="4" style="border-bottom: 1px solid black"></td></tr></table>
Related