What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Stargazer Package in R (with Examples)

  • Jan 07, 2024
  • 7 Minutes Read
Stargazer Package in R (with Examples)

R Stargazer has gained popularity for its ability to present summary statistics in a visually appealing and customizable format. Its' ability to create statistical tables with proper formatting has made it famous among data scientists and analysts. We will learn about the Stargazer in R in this article, looking at its uses in Markdown compatibility, LaTeX integration, and summary statistics.

What is Stargazer in R? 

Stargazer is an R package designed to generate well-formatted statistical tables. Whether you're using hypothesis testing, regression models, or summary statistics, Stargazer makes it easier to present your results in a way that is suitable for being released. It is a flexible tool for data analysts, supporting a large variety of models and statistical outputs.

Here are a few points on why it's so popular in data-driven communities:

  • Easy Integration: Stargazer works well with many different statistical models in R, such as lm, glm, and numerous others. It is therefore the preferred option for users handling various kinds of data.
  • Customization: Stargazer gives users the ability to personalize the output tables to suit their tastes. The package offers a great deal of flexibility in terms of choosing the variables to display as well as the style of the table.
  • Latex and HTML Support: Support for HTML and LaTeX in Stargazer is one of its best features. Researchers who wish to publish their statistical results in academic papers or online journals will find this especially helpful.

Now, let's explore some key functionalities of "stargazer" in R.

Summary Statistics with Stargazer

Let's look at how we can summarize statistical learnings with stargazer in R.

Basic Summary Statistics

Using the "stargazer" library to generate the summary statistics for your data is one of its fundamental uses. Now let's look at a straightforward example using R's built-in mtcars dataset:


stargazer(mtcars, type = "text")



Statistic N   Mean   St. Dev.  Min     Max  
mpg       32 20.091   6.027   10.400 33.900 
cyl       32  6.188   1.786     4       8   
disp      32 230.722 123.939  71.100 472.000
hp        32 146.688  68.563    52     335  
drat      32  3.597   0.535   2.760   4.930 
wt        32  3.217   0.978   1.513   5.424 
qsec      32 17.849   1.787   14.500 22.900 
vs        32  0.438   0.504     0       1   
am        32  0.406   0.499     0       1   
gear      32  3.688   0.738     3       5   
carb      32  2.812   1.615     1       8   


This simple use of "stargazer" yields a clean table with important statistics, such as mean, standard deviation, minimum, and maximum values, for each variable in the dataset.

Customizing Summary Tables

While the default summary table is informative, the Stargazer library offers plenty of customization options for the summary table. You can choose to display specific variables, change the style of the table, and include additional statistics.

Let's look at an example:

stargazer(mtcars[, c("mpg", "hp", "wt")], 
          summary.stat = c("mean", "min", "max", "p25", "p75"),
          title = "Customized Summary Statistics",
          type = "html", out = "my_table.html")



Customized Summary Statistics
Statistic Mean Min Max Pctl(25) Pctl(75)
mpg 20.091 10.400 33.900 15.425 22.800
hp 146.688 52 335 96.5 180
wt 3.217 1.513 5.424 2.581 3.610


In this example, we've selected specific variables (mpg, hp, and wt) and specified the summary statistics to display. We can select the table's appearance using the style argument; in this particular case, we've chosen an "HTML" style.

Stargazer in LaTeX

Let us now look at how we can use Stargazer in the famous LaTeX typesetting system.

LaTeX is a typesetting system commonly used for the production of scientific and mathematical documents. LaTeX is frequently chosen by academics and researchers due to its strong handling of mathematical equations and its ability to generate documents of exceptional quality.

For researchers who wish to incorporate statistical results into their LaTeX publications, Stargazer is an invaluable resource because it integrates with LaTeX with ease.

The way to do that is also very simple:

latex_code <- stargazer(mtcars, title = "Summary Statistics", type = "latex")


When this code snippet is included in a LaTeX document, it generates LaTeX code that renders a table showing the mtcars dataset's summary statistics. You can copy and paste the generated LaTeX code straight into your LaTeX document.

Stargazer in R Markdown

Markdown is a famous way to combine text, code, and output in a single document. Let us now see how Stargazer integrates itself with Markdown.

R Markdown is an open-source format where the output, code, and text are all combined into one document. It is often utilized for dynamic reporting and reproducible research. You can combine narrative text and R code with R Markdown to create a comprehensive document that summarises your analysis and its findings.

"Stargazer" is well-suited for integration into R Markdown documents. It allows you to include dynamic and interactive tables that update automatically when the underlying data or code changes:

stargazer(mtcars[, c("mpg", "hp", "wt")], 
          summary.stat = c("mean", "min", "max", "p25", "p75"),
          title = "Customized Summary Statistics",
          type = "html",
          out = "custom_stats.html")


This will output the table in an HTML format which you can easily integrate with the markdown file in R. This will help you such that when you knit the document, the customized table will be included in the output.


Overall, Stargazer in R is a flexible and effective tool for displaying summary statistics in an easily customized format. By exploring the functionalities of "stargazer" in summary statistics, LaTeX integration, and R Markdown compatibility, you can unlock new possibilities for sharing your data-driven insights with the world.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abhisek Ganguly
Passionate machine learning enthusiast with a deep love for computer science, dedicated to pushing the boundaries of AI through academic research and sharing knowledge through teaching.