yrx1702
yrx1702

Reputation: 1641

stargazer() captions in multiple tables

I have a data set with some variables to summarize and a group membership indicator

df <- data.frame(var1 = rnorm(100), 
                 var2 = rnorm(100), 
                 group = rep(c("A","B"), each = 50))

where I want to generate summary tables for groups A and B separately. We can do this using dplyr and purrr as follows:

library(purrr)
library(dplyr)

df %>% split(. $group) %>% walk(~ stargazer(., type = "text", title = unique(df$group)))


A
==========================================================
Statistic N  Mean  St. Dev.  Min   Pctl(25) Pctl(75)  Max 
----------------------------------------------------------
var1      50 0.371  1.044   -2.350  -0.251   1.043   2.545
var2      50 0.014  0.849   -2.212  -0.407   0.584   1.710
----------------------------------------------------------

A
===========================================================
Statistic N   Mean  St. Dev.  Min   Pctl(25) Pctl(75)  Max 
-----------------------------------------------------------
var1      50 0.016   1.246   -2.432  -1.107   0.879   2.551
var2      50 -0.031  0.975   -2.713  -0.682   0.465   2.022
-----------------------------------------------------------

where I naively tried to introduce the group names as table captions by handing over unique(df$group) to the title parameter of stargazer(). When we do this, only the first element of the provided vector of table captions is used and all tables therefore have caption A. Is there a solution that results in the correct table captions?

Upvotes: 0

Views: 528

Answers (2)

ToWii
ToWii

Reputation: 660

You can do this by simply changing your existing code as follows:

df %>% split(. $group) %>% walk(~ stargazer(., type = "text", title = .$group))

Replace unique(df$group) by .$group and it will work. Don't forget to add the library call for stargazer. The reason is that by using unique(df$group) you pass a character veector of length two to stargazer and by definition it only takes the first argument. By using .$group it depends on the respective group and you obtain the right title.

library(purrr)
library(dplyr)
library(stargazer)

df <- data.frame(var1 = rnorm(100), 
                 var2 = rnorm(100), 
                 group = rep(c("A","B"), each = 50))

df %>% split(. $group) %>% walk(~ stargazer(., type = "text", title = .$group))
    
A
===========================================================
Statistic N   Mean  St. Dev.  Min   Pctl(25) Pctl(75)  Max 
-----------------------------------------------------------
var1      50 -0.122  1.019   -3.582  -0.671   0.511   2.223
var2      50 0.082   1.077   -2.780  -0.702   0.841   2.203
-----------------------------------------------------------

B
===========================================================
Statistic N   Mean  St. Dev.  Min   Pctl(25) Pctl(75)  Max 
-----------------------------------------------------------
var1      50 0.023   0.971   -2.431  -0.706   0.609   2.299
var2      50 -0.041  1.039   -3.807  -0.626   0.718   1.950
-----------------------------------------------------------

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 388982

You can use iwalk which will pass data as well as name of the list.

library(tidyverse)
library(stargazer)

df %>% 
  split(. $group) %>% 
  iwalk(~stargazer(.x, type = "text", title = .y))

A
===========================================================
Statistic N   Mean  St. Dev.  Min   Pctl(25) Pctl(75)  Max 
-----------------------------------------------------------
var1      50 -0.064  0.890   -2.265  -0.744   0.734   1.699
var2      50 -0.130  1.003   -2.404  -0.752   0.581   1.930
-----------------------------------------------------------

B
==========================================================
Statistic N  Mean  St. Dev.  Min   Pctl(25) Pctl(75)  Max 
----------------------------------------------------------
var1      50 0.086  0.823   -1.737  -0.476   0.742   1.736
var2      50 0.167  1.178   -1.709  -0.670   0.898   2.677
----------------------------------------------------------

Upvotes: 1

Related Questions