Stargazer descriptive table does not calculate data in R

I am trying to create a descriptive table (to export to LaTeX) with Stargazer. For some reason, in some datasets, like the one below, it only yields the first line (the column titles of the table).

I am not sure why this happens. It seems there is something wrong with my data. I saw a similar problem here, but the desired table was not of summary statistics. In my case, if I do the solution presente there (turn the dataset into a matrix), the table yielded is of the entire dataset, not descriptive stats.

Below is a code for a sample data and what I tried to produce the tables.

df <- structure(list(country = structure(c(26L, 5L, 26L, 25L, 25L, 
3L, 27L, 27L, 27L, 6L, 27L, 26L, 6L, 10L, 1L, 5L, 27L, 20L, 27L, 
10L, 26L, 1L, 2L, 15L, 10L, 1L, 26L, 17L, 9L, 7L, 11L, 1L, 26L, 
20L, 27L, 6L, 1L, 25L, 9L, 2L, 22L, 15L, 23L, 27L, 9L, 27L, 8L, 
27L, 19L, 24L, 20L, 19L, 22L, 26L, 26L, 10L, 10L, 16L, 20L, 25L, 
2L, 26L, 1L, 27L, 11L, 1L, 2L, 8L, 25L, 27L, 26L, 9L, 10L, 4L, 
9L, 27L, 7L, 19L, 27L, 26L, 6L, 17L, 11L, 16L, 16L, 26L, 27L, 
26L, 27L, 14L, 23L, 27L, 23L, 10L, 9L, 23L, 22L, 26L, 26L, 27L
), .Label = c("Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic", 
"Denmark", "Estonia", "Finland", "France", "Germany", "Greece", 
"Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", 
"Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", 
"Slovenia", "Spain", "Sweden", "United Kingdom"), class = "factor"), 
    ptyname_eng = structure(c("Centre Party", "Green Party", 
    "Moderate Party", "Europe of the Peoples-The Greens", "Europe of the Peoples-The Greens", 
    "Order, Lawfulness, and Justice (RZS)", "Labour", "Green Party", 
    "Labour", "Liberal Party", "Independent", "Liberal People's Party", 
    "Danish People's Party", "Free Democratic Party", "Austrian People's Party", 
    "Christian and Democratic Union-Czechoslovak People's Party", 
    "Green Party", "Coalition Agreement for the Future - CenterLeft", 
    "UK Independence Party (UKIP)", "Free Democratic Party", 
    "Centre Party", "The Greens", "Ecolo", "Libertas", "Free Voters", 
    "The Greens", "Centre Party", "Christian Social People's Party", 
    "Europe Écologie", "People's Union of Estonia", "Action", 
    "Communist Party of Austria", "Centre Party", "Coalition Agreement for the Future - CenterLeft", 
    "Jury Team", "Social Democrats", "Austrian People's Party", 
    "Union, Progress, and Democracy", "The Left Front (Left Party + French Communist Party)", 
    "Ecolo", "Greater Romania Party", "Harmony Centre", "Green Party", 
    "Green Party", "Socialist Party", "No2EU: Yes to Democracy", 
    "Social Democratic Party of Finland", "Conservatives", "Libertas", 
    "For Real", "Civic Platform", "Christian Union-Reformed Political Party", 
    "Democratic Liberal Party", "Sweden Democrats", "Green Party", 
    "Free Democratic Party", "Christian Democratic Union/Christian Social Union", 
    "Civic Democratic Party", "Civic Platform", "Union, Progress, and Democracy", 
    "Christian Democratic and Flemish Party", "Pirate Party", 
    "The Greens", "Socialist Labour Party SLP", "New Democracy", 
    "The Greens", "Christian Democratic and Flemish Party", "Left Alliance", 
    "Union, Progress, and Democracy", "British National Party (BNP)", 
    "Left Party", "The Left Front (Left Party + French Communist Party)", 
    "Christian Social Democrats", "Progressive Party of Working People", 
    "Socialist Party", "UK Independence Party (UKIP)", "Social Democratic Party", 
    "Democrats 66", "Mebyon Kernow", "June List", "Socialist People's Party", 
    "Christian Social People's Party", "New Democracy", "Frontas Party", 
    "Homeland Union - Lithuanian Christian Democrats", "Liberal People's Party", 
    "No2EU: Yes to Democracy", "Centre Party", "Pro Democracy: Libertas.eu", 
    "Anticapitalist List", "Conservative Democrats of Slovakia", 
    "Christian Party - Christian People's Alliance", "Slovak Democratic and Christian Union", 
    "Free Democratic Party", "Europe Écologie", "Direction - Social Democracy", 
    "Democratic Union of Hungarians in Romania", "Centre Party", 
    "Liberal People's Party", "Conservatives"), label = "Party name (in English)", format.stata = "%75s"), 
    votes = c(5.47, 2.06, 18.83, 2.49, 2.49, 4.67, 15.7, 8.6, 
    15.7, 20.23, NA, 13.58, 15.28, 11, 29.98, 7.64, 8.6, 2.44, 
    16.5, 11, 5.47, 9.93, 8.64, 4.3, 1.7, 9.93, 5.47, 31.32, 
    16.28, 2.2, 0.76, 0.66, 5.47, 2.44, 0.5, 21.49, 29.98, 2.85, 
    6.05, 8.64, 8.65, 19.57, 2.11, 8.6, 16.48, 1.01, 17.5, 27.7, 
    0.32, 9.76, 44.43, 6.82, 29.71, 3.27, 11.02, 11, 30.7, 1.35, 
    44.43, 2.85, 14.43, 7.13, 9.93, 1.1, 32.3, 9.93, 14.43, 5.9, 
    2.85, 6.2, 5.66, 6.05, 7.2, 34.9, 16.48, 16.5, 8.7, 11.32, 
    0.1, 3.55, 15.86, 31.32, 32.3, 2.43, 26.86, 13.58, 1.01, 
    5.47, 0.5, 3.39, 2.1, 1.6, 16.98, 11, 16.28, 32.01, 8.92, 
    5.47, 13.58, 27.7), seats = c(1, 0, 4, 1, 1, 0, 13, 2, 13, 
    3, NA, 3, 2, 12, 6, 2, 2, 0, 13, 12, 1, 2, 2, 0, 0, 2, 1, 
    3, 14, 0, 0, 0, 1, 0, 0, 4, 6, 1, 4, 2, 3, 2, 0, 2, 14, 0, 
    2, 25, 0, 1, 25, 2, 10, 0, 2, 12, 34, 0, 25, 1, 3, 1, 2, 
    0, 8, 2, 3, 0, 1, 2, 1, 4, 8, 2, 14, 13, 1, 3, 0, 0, 2, 3, 
    8, 0, 4, 3, 0, 1, 0, 0, 0, 0, 2, 12, 14, 5, 3, 1, 3, 25), 
    lsq = c(5.65121280548163, 11.0409569967897, 5.65121280548163, 
    2.09070598337411, 2.09070598337411, 18.4291883786975, 7.64222053188085, 
    7.64222053188085, 7.64222053188085, 8.49009926343377, NA, 
    5.65121280548163, 8.49009926343377, 4.03227351136326, 5.21894849144935, 
    11.0409569967897, 7.64222053188085, 4.87412556260095, 7.64222053188085, 
    4.03227351136326, 5.65121280548163, 5.21894849144935, 4.16813185258047, 
    9.99651439252703, 4.03227351136326, 5.21894849144935, 5.65121280548163, 
    14.683991850538, 10.16545, 10.829844309951, 3.4795321239576, 
    5.21894849144935, 5.65121280548163, 4.87412556260095, 7.64222053188085, 
    8.49009926343377, 5.21894849144935, 2.09070598337411, 10.1654489717407, 
    4.16813185258047, 2.66691671130863, 9.99651439252703, 7.88621704182489, 
    7.64222053188085, 10.1654489717407, 7.64222053188085, 6.48086623985829, 
    7.64222053188085, 3.63340749159794, 10.4201368043039, 4.87412556260095, 
    3.63340749159794, 2.66691671130863, 5.65121280548163, 5.65121280548163, 
    4.03227351136326, 4.03227351136326, 8.7655582315738, 4.87412556260095, 
    2.09070598337411, 4.16813185258047, 5.65121280548163, 5.21894849144935, 
    7.64222053188085, 3.4795321239576, 5.21894849144935, 4.16813185258047, 
    6.48086623985829, 2.09070598337411, 7.64222053188085, 5.65121280548163, 
    10.1654489717407, 4.03227351136326, 6.81012399952372, 10.1654489717407, 
    7.64222053188085, 10.829844309951, 3.63340749159794, 7.64222053188085, 
    5.65121280548163, 8.49009926343377, 14.683991850538, 3.4795321239576, 
    8.7655582315738, 8.7655582315738, 5.65121280548163, 7.64222053188085, 
    5.65121280548163, 7.64222053188085, 6.15555836341693, 7.88621704182489, 
    7.64222053188085, 7.88621704182489, 4.03227351136326, 10.16545, 
    7.88621704182489, 2.66691671130863, 5.65121280548163, 5.65121280548163, 
    7.64222053188085), v020_03 = c(5, 5, 3, 5, 5, 3, 5, 5, 3, 
    NA, 5, 5, 3, 4, 3, 2, 5, NA, 4, 5, 5, 5, 5, NA, 2, 5, 4, 
    5, 5, 2, NA, NA, 5, 5, 2, 5, 3, 4, 5, 5, 3, 5, 5, 5, 5, 5, 
    5, 3, 3, NA, 2, 2, 3, 1, 5, 5, 1, 3, 3, NA, 1, 5, 5, 5, 3, 
    5, 5, 5, 4, 2, NA, 5, 1, 3, 2, 3, NA, 5, 5, 4, NA, 4, 2, 
    NA, 2, 5, 5, 5, 3, 5, 2, 1, 3, 3, 5, NA, 4, 5, 5, 1), v020_04 = c(5, 
    4, NA, 1, 3, 5, 3, 2, 2, NA, 2, 4, 4, 5, 5, 2, 1, NA, 4, 
    5, 5, 2, 5, NA, 3, 3, 4, 4, 3, 4, NA, NA, 3, 3, 4, 1, 5, 
    4, 2, 5, 1, 5, 3, 4, 2, 1, 4, 5, 3, NA, 3, 4, 4, 4, 3, 5, 
    5, 2, 4, NA, 1, 1, 3, 1, 3, 2, 2, 2, 3, 3, NA, 1, 1, 3, 2, 
    4, NA, 5, 2, 3, NA, 5, 2, NA, 2, 2, 1, 4, 5, 3, 4, 3, 4, 
    5, 1, NA, 3, 2, 4, 5), v020_08 = c(5, 3, 3, 1, 3, 5, 3, 1, 
    2, NA, 2, 3, 4, 5, 5, 5, 1, NA, 5, 5, NA, 2, 3, NA, 4, 2, 
    3, 4, 1, 3, NA, NA, 3, 2, 3, 1, 4, 2, 1, 3, 1, 2, 3, 2, 1, 
    1, 3, 3, 3, NA, 4, 2, 4, 3, 2, 2, 5, 3, 4, NA, 2, 3, 1, 1, 
    3, 3, 2, 2, 3, 3, NA, 1, 5, 4, 1, 3, NA, 4, 2, 3, NA, 2, 
    3, NA, 2, 4, 1, 4, 1, 1, 4, 3, 4, 4, 1, NA, 5, 5, 3, 3), 
    v018_1 = c(8, 5, 5, 1, 3, 9, 3, 2, 2, NA, NA, 5, 5, 5, 7, 
    7, 1, NA, 7, 5, 6, 4, 3, NA, 8, 4, 5, 6, 0, 5, NA, NA, 5, 
    2, 7, 3, 8, 4, 0, 4, 0, 7, 7, NA, 0, 0, 3, 8, 5, NA, 7, 5, 
    10, 8, 4, 5, 8, 5, 8, NA, 5, 2, 0, 10, 6, 3, 3, 2, 4, 6, 
    NA, 0, 8, 4, 0, 6, NA, 4, 3, 5, NA, 7, 6, NA, 9, 8, 0, 6, 
    5, 0, 8, 6, 8, 5, 2, NA, 6, 5, 7, 7), v020_05 = c(1, 1, 1, 
    1, 3, 1, 1, 1, 2, NA, 1, 2, 4, 2, 4, 3, 1, NA, 3, 2, 1, 1, 
    4, NA, 2, 1, 2, 3, 1, 1, NA, NA, 2, 1, 1, 1, 2, 4, 1, 2, 
    1, 1, 1, 1, 1, 1, 1, 1, 3, NA, 5, 4, 1, 4, 2, 2, 1, 4, 2, 
    NA, 3, 1, 1, 1, 1, 1, 2, 1, 3, 4, NA, 1, 4, 3, 1, 3, NA, 
    1, 1, 2, NA, 4, 1, NA, 4, 2, 1, 1, 3, 1, 4, 2, 2, 4, 1, NA, 
    1, 2, 1, 5), v020_02 = c(1, 3, 1, 5, 4, 1, 3, 5, 4, NA, 3, 
    2, 3, 2, 1, 3, 5, NA, 1, 1, 1, 5, 4, NA, 1, 2, 2, 1, 5, 2, 
    NA, NA, 3, 3, 1, 5, 3, 2, 5, 4, 1, 2, 2, 5, 5, 5, 3, 3, 1, 
    NA, 1, 4, 1, 2, 4, 1, 1, 3, 1, NA, 3, 4, 4, 4, 3, 4, 5, 4, 
    2, 3, NA, 5, 1, 2, 2, 1, NA, 2, 3, 2, NA, 1, 2, NA, 2, 2, 
    5, 2, 1, 4, 2, 4, 2, 2, NA, NA, 2, 3, 1, 1)), row.names = c(NA, 
-100L), class = c("tbl_df", "tbl", "data.frame"))

stargazer (df)
stargazer(as.matrix(df)
df <- as.matrix(df)
stargazer(df)

Upvotes: 0

Views: 68

Answers (2)

akrun
akrun

Reputation: 887118

We can also convert to tibble

stargazer::stargazer(as_tibble(df))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388982

Convert the data to dataframe.

stargazer::stargazer(data.frame(df))

With type = 'text'

stargazer::stargazer(data.frame(df), type = 'text')

===========================================================
Statistic N   Mean  St. Dev.  Min  Pctl(25) Pctl(75)  Max  
-----------------------------------------------------------
votes     99 11.784  10.443  0.100  3.330    16.380  44.430
seats     99 4.515   6.649   0.000  0.500    4.000   34.000
lsq       99 6.627   2.903   2.091  4.521    7.886   18.429
v020_03   88 3.852   1.352   1.000  3.000    5.000   5.000 
v020_04   87 3.172   1.340   1.000  2.000    4.000   5.000 
v020_08   87 2.828   1.278   1.000  2.000    4.000   5.000 
v018_1    86 4.767   2.660   0.000  3.000    7.000   10.000
v020_05   88 1.966   1.198   1.000  1.000    3.000   5.000 
v020_02   87 2.667   1.403   1.000  1.000    4.000   5.000 
-----------------------------------------------------------

Upvotes: 1

Related Questions