Search code examples
rlatexstargazer

Stargazer summary table is empty


I want to make a summary-stargazer table in R, which gives me the respective number of observations N, the mean, the standard deviation, minimum and maximum for the variables "educ" and "exper". I used the following code in order to generate a table in Latex.

rm(list = ls())
wage2 <- read_csv("~/homework/wage2.txt")
library(stargazer)

stargazer(wage2[c("educ","exper")], type = "latex", digits=1,flip = TRUE)

I get the following output. As you can see the table appears, but it is empty. If I use standard R-data (for example data("mtcars")), it works. Anyone has an idea, what could be wrong with my data? The usual R command summary(wage2) works perfectly, but I can not use this in Latex. Thank you!

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University.
E-mail: hlavac at fas.harvard.edu 
% Date and time: Mo, Mai 07, 2018 - 16:40:31  
\begin{table}[!htbp] \centering  
  \caption{}  
  \label{}  
\begin{tabular}{@{\extracolsep{5pt}}lcc}  
\\[-1.8ex]\hline  
\hline \\[-1.8ex]  
Statistic \\  
\hline \\[-1.8ex]  
N \\  
Mean \\  
St. Dev. \\  
Min \\  
Max \\  
\hline \\[-1.8ex]  
\end{tabular}  
\end{table}

The data looks like this:

> str(wage2)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   935 obs. of  17 variables:
 $ wage   : int  769 808 825 650 562 1400 600 1081 1154 1000 ...
 $ hours  : int  40 50 40 40 40 40 40 40 45 40 ...
 $ IQ     : int  93 119 108 96 74 116 91 114 111 95 ...
 $ KWW    : int  35 41 46 32 27 43 24 50 37 44 ...
 $ educ   : int  12 18 14 12 11 16 10 18 15 12 ...
 $ exper  : int  11 11 11 13 14 14 13 8 13 16 ...
  - attr(*, "spec")=List of 2
  ..$ cols   :List of 17
  .. ..$ wage   : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ hours  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ IQ     : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ KWW    : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ educ   : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
  .. ..$ exper  : list()
  .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
    .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
  ..$ default: list()
  .. ..- attr(*, "class")= chr  "collector_guess" "collector"
  ..- attr(*, "class")= chr "col_spec"
> head(wage2)
# A tibble: 6 x 17
   wage hours    IQ   KWW  educ exper tenure   age married black south
urban  sibs brthord
  <int> <int> <int> <int> <int> <int>  <int> <int>   <int> <int> <int>
<int> <int>   <int>
1   769    40    93    35    12    11      2    31       1     0     0  
1     1       2
2   808    50   119    41    18    11     16    37       1     0     0    
1     1      NA
3   825    40   108    46    14    11      9    33       1     0     0    
1     1       2
4   650    40    96    32    12    13      7    32       1     0     0  
1     4       3
5   562    40    74    27    11    14      5    34       1     0     0    
1    10       6
6  1400    40   116    43    16    14      2    35       1     1     0    
1     1       2
# ... with 3 more variables: meduc <int>, feduc <int>, lwage <dbl>

Solution

  • To generate a summary, stargazer requires a data.frame as input. As you are supplying a tibble, stargazer cannot produce any output.

    Adding as.data.frame() should do the trick **.

    stargazer(as.data.frame(wage2[c("educ","exper")]), type = "latex", digits=1,flip = TRUE)
    

    ** Disclaimer: As you are not supplying a reproduceable example I created a random tibble to reproduce your error, and tried the same code again with as.data.frame() which produced a satisfactory result. But I cannot "guarantee" that your specific example does not suffer from further specification problems.