I have a problem with my stargazer output in R.
Here's my original dataframe:
Rank p LMax 10% 5% 1%
var1 0.427 24.25 21.8 27.4 31.5
var2 0.228 7.23 11.5 12.2 16.7
What stargazer creates:
stargazer(data_summary, summary=FALSE, digits = 3)
Rank p LMax 10% 5% 1%
var1 0.427 24.250 21.800 27.400 31.500
var2 0.248 7.230 11.500 12.200 16.700
It's important to keep the three digits for p but remain 2 digits for the others. digits = 2 doesn't solve the problem, as p then only has two digits.
The desired output:
Rank p LMax 10% 5% 1%
var1 0.427 24.25 21.80 27.40 31.50
var2 0.248 7.23 11.50 12.20 16.70
Any idea how to solve this?
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
# ------------------------------------
来源:https://stackoverflow.com/questions/40015431/r-stargazer-different-decimals