You can either use split
+ lapply
from base R:
library(stargazer)
lapply(split(df, df$year), stargazer, type = "text")
or by
:
by(df, df$year, stargazer, type = 'text')
Result:
===============================================================
Statistic N Mean St. Dev. Min Max
---------------------------------------------------------------
Population 10 9,083,988.000 7,541,970.000 491,723 21,759,420
Distance..km. 10 5,637.500 2,385.941 2,211 9,500
year 10 2,008.000 0.000 2,008 2,008
---------------------------------------------------------------
===============================================================
Statistic N Mean St. Dev. Min Max
---------------------------------------------------------------
Population 10 9,361,404.000 7,798,880.000 496,963 22,549,547
Distance..km. 10 5,637.500 2,385.941 2,211 9,500
year 10 2,009.000 0.000 2,009 2,009
---------------------------------------------------------------
===============================================================
Statistic N Mean St. Dev. Min Max
---------------------------------------------------------------
Population 10 9,645,370.000 8,065,676.000 502,384 23,369,131
Distance..km. 10 5,637.500 2,385.941 2,211 9,500
year 10 2,010.000 0.000 2,010 2,010
---------------------------------------------------------------
df$year: 2008
[1] ""
[2] "==============================================================="
[3] "Statistic N Mean St. Dev. Min Max "
[4] "---------------------------------------------------------------"
[5] "Population 10 9,083,988.000 7,541,970.000 491,723 21,759,420"
[6] "Distance..km. 10 5,637.500 2,385.941 2,211 9,500 "
[7] "year 10 2,008.000 0.000 2,008 2,008 "
[8] "---------------------------------------------------------------"
--------------------------------------------------------------------------
df$year: 2009
[1] ""
[2] "==============================================================="
[3] "Statistic N Mean St. Dev. Min Max "
[4] "---------------------------------------------------------------"
[5] "Population 10 9,361,404.000 7,798,880.000 496,963 22,549,547"
[6] "Distance..km. 10 5,637.500 2,385.941 2,211 9,500 "
[7] "year 10 2,009.000 0.000 2,009 2,009 "
[8] "---------------------------------------------------------------"
--------------------------------------------------------------------------
df$year: 2010
[1] ""
[2] "==============================================================="
[3] "Statistic N Mean St. Dev. Min Max "
[4] "---------------------------------------------------------------"
[5] "Population 10 9,645,370.000 8,065,676.000 502,384 23,369,131"
[6] "Distance..km. 10 5,637.500 2,385.941 2,211 9,500 "
[7] "year 10 2,010.000 0.000 2,010 2,010 "
[8] "---------------------------------------------------------------"
The disadvantage of these two methods is that they print out the tables twice (once from stargazer
output, another from lapply
/by
). To get around this, you can use walk
form purrr
to only call stargazer
for it's side-effects:
library(dplyr)
library(purrr)
df %>%
split(.$year) %>%
walk(~ stargazer(., type = "text"))
Result:
===============================================================
Statistic N Mean St. Dev. Min Max
---------------------------------------------------------------
Population 10 9,083,988.000 7,541,970.000 491,723 21,759,420
Distance..km. 10 5,637.500 2,385.941 2,211 9,500
year 10 2,008.000 0.000 2,008 2,008
---------------------------------------------------------------
===============================================================
Statistic N Mean St. Dev. Min Max
---------------------------------------------------------------
Population 10 9,361,404.000 7,798,880.000 496,963 22,549,547
Distance..km. 10 5,637.500 2,385.941 2,211 9,500
year 10 2,009.000 0.000 2,009 2,009
---------------------------------------------------------------
===============================================================
Statistic N Mean St. Dev. Min Max
---------------------------------------------------------------
Population 10 9,645,370.000 8,065,676.000 502,384 23,369,131
Distance..km. 10 5,637.500 2,385.941 2,211 9,500
year 10 2,010.000 0.000 2,010 2,010
---------------------------------------------------------------
Note:
All methods above works for latex output (type = "latex"
). I only set type = "text"
for demonstrative purposes.
stargazer
is a package for printing well-formatted tables, not an analysis tool, so your question of "R should start over to analyse after every 49th row." does not make any sense. – Negretedput(my_data)
into your question. – Negretedput(my_data)
version instead of what you have here. Also read MrFlick's link on how to provide a minimal reproducible example – Negrete