1

I want the data output in table form, but I'm struggling to align the values in a way that disregards the minus sign in front of negative values. Been researching for a while to no avail. Hopefully there's a simple solution.

Please see included code sample and image link below.

#install.packages("stargazer")
library(stargazer)

df<-as.data.frame(array(data=c(1.33,-1.23,23.4343,-3000)))
  
stargazer(df, type = "text", 
          title            = "Align me plz",
          rownames         = FALSE,
          colnames         = FALSE,
          summary          = FALSE
)

Screenshot of the table

Sindre
  • 11
  • 2
  • Welcome to SO, Sindre! zx8754 just suggested an edit for your question that changed `require` to `library`, which has a not-insignificant difference, look at https://stackoverflow.com/a/51263513/3358272. Long-story-short, if you use `require`, you really need to capture it's return value and do something with it, otherwise your code is fragile. (I second the comment asking for the linked image.) – r2evans Feb 24 '21 at 21:58
  • Thanks for the input, guys. Hopefully I've successfully added the image now. Looking forward to more insights. – Sindre Feb 24 '21 at 22:07
  • Seems like the `align` argument may be relevant: `align`: a logical value indicating whether numeric values in the same column should be _aligned at the decimal mark_ in LaTeX output. Requires `\usepackage{dcolumn}` in LaTeX preamble." – Henrik Feb 24 '21 at 22:10
  • Thanks for the answer, Henrik. It seems dcolumn is a Latex package. I'm trying to figure out how to implement that in R studios. – Sindre Feb 24 '21 at 22:19
  • 1
    What is the output you ultimately want. Do you want it to be decimal aligned, or do you just want the positive numbers pushed to the right one space? – DaveArmstrong Feb 24 '21 at 23:32
  • @DaveArmstrong, I would like them to be decimal aligned. – Sindre Feb 25 '21 at 08:07

1 Answers1

0

This doesn't solve the problem, but perhaps points to the culprit. First, it doesn't look like there is an alignment option when the output is "text". I tried to force the alignment changing the numbers into character strings as follows:

df<-data.frame(x=c(1.33,-1.23,23.4343,-3000))

format_decimal <- function(x, digits=2){
  s <- stringr::str_split(as.character(x), "\\.", simplify=TRUE)
  mchar <- apply(nchar(s), 2, max)
  sprintf(paste0("%", mchar[1]+digits, ".", digits, "f"), x)
}
library(dplyr)
df <- df %>% mutate(across(where(is.numeric), format_decimal))

When you print the data frame (outside of stargazer), you see that the values are left-aligned (which makes them decimal aligned when you print a fixed number of decimal places).

df
#          x
# 1     1.33
# 2    -1.23
# 3    23.43
# 4 -3000.00

However, when you use the same data in stargazer, the data are now centered.

stargazer(df, type = "text", 
          title            = "Align me plz",
          rownames         = FALSE,
          colnames         = FALSE,
          summary          = FALSE
)

# Align me plz
# ========
#   1.33  
#  -1.23    
#  23.43  
# -3000.00
# --------

So, it looks like the internals of the function trim and pad the values so they are centered. To solve the problem, you would likely have to fiddle with the internals of the package. There is a function that gets defined in there called .trim which shaves off leading and trailing spaces. I tried to re-define that as .trim = function(x)x, but that didn't solve the problem either. So it might be that changing that in coordination with changing something else or changing something else entirely is required.

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25