R stargazer: Different decimals

。_饼干妹妹 提交于 2019-12-01 10:47:00

A hacky solution is to convert your dataframe into character vectors, each with the appropriate number of decimal points.

data_summary <- read.table(text = "
Rank  p     LMax   p10   p5  p1   
var1  0.427  24.25 21.8 27.4 31.5
var2  0.228  7.23  11.5 12.2 16.7", header = TRUE, stringsAsFactors = FALSE)

We write an anonymous function that, given an integer x (number of decimal points) and a numeric vector y, returns a character vector:

out <- mapply(function(x, y) sprintf(sprintf("%%.0%if", y), x), 
  data_summary[-1],
  c(3, 2, 2, 2, 2))
#      p       LMax    p10     p5      p1     
# [1,] "0.427" "24.25" "21.80" "27.40" "31.50"
# [2,] "0.228" "7.23"  "11.50" "12.20" "16.70"

Bind the values to row labels:

data_summary_out <- as.data.frame(cbind(data_summary[, 1], out))
data_summary_out <- setNames(data_summary_out, names(data_summary))

stargazer now gives you your desired output:

library(stargazer)
stargazer(data_summary_out,
          type = "text",
          summary = FALSE,
          digits = NA)
# ====================================
#   Rank   p   LMax   p10   p5    p1  
# ------------------------------------
# 1 var1 0.427 24.25 21.80 27.40 31.50
# 2 var2 0.228 7.23  11.50 12.20 16.70
# ------------------------------------
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!