微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Stargazer描述表不计算R中的数据

如何解决Stargazer描述表不计算R中的数据

我正在尝试使用 Stargazer 创建一个描述表(以导出到 LaTeX)。出于某种原因,在某些数据集中,例如下面的数据集,它只生成第一行(表格的列标题)。

我不确定为什么会发生这种情况。我的数据似乎有问题。我看到了类似的问题 here,但所需的表不是汇总统计数据。就我而言,如果我执行那里的解决方案(将数据集转换为矩阵),则生成的表是整个数据集,而不是描述性统计数据。

以下是示例数据的代码以及我尝试生成表格的内容

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

解决方法

将数据转换为数据框。

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

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 
-----------------------------------------------------------
,

我们也可以转换为tibble

stargazer::stargazer(as_tibble(df))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。