Does stargazer interpreting data.frame data as latex code constitute a bug or is this intended?
Asked Answered
M

1

7

I am experiencing a problem where the Stargazer function is interpreting data in my data.frame as a latex command. I would like to find a way to suppress this feature of stargazer. See below.

z <- c("Bank of America Corp",
    "Citigroup Inc",
    "JPMorgan Chase & Co",
      "Morgan Stanley",
     "Wells Fargo & Co")

 s <- data.frame(z=z, l= c(100000, 25, 10000, 24, 100000))
 library(stargazer)
 stargazer(s, type = "text", summary = FALSE)
 # prints out
     ==============================
               z              l   
     ------------------------------
      1 Bank of America Corp 100,000
      2    Citigroup Inc       25   
      3    JPMorgan Chase      Co   
      4    Morgan Stanley      24   
      5     Wells Fargo        Co   
     ------------------------------

Here the ampersand is causing a new column to be assumed due to its meaning in latex. I confirmed this because replacing & with and causes the table to print out properly.

  z <- c("Bank of America Corp",
    "Citigroup Inc",
    "JPMorgan Chase and Co",
      "Morgan Stanley",
     "Wells Fargo and Co")

 s <- data.frame(z=z, l= c(100000, 25, 10000, 24, 100000))
 library(stargazer)
 stargazer(s, type = "text", summary = FALSE)
 # prints out

 ===============================
             z              l   
 -------------------------------
 1 Bank of America Corp  100,000
 2     Citigroup Inc       25   
 3 JPMorgan Chase and Co 10,000 
 4    Morgan Stanley       24   
 5  Wells Fargo and Co   100,000
 -------------------------------

Is there any option I can invoke in the stargazer function to prevent this behavior?

Maricela answered 15/4, 2015 at 21:23 Comment(1)
which version of latex and stargazer are you using?Borodin
B
4

There does not seem to be an option for this with the current version of stargazer. If you check the source code you will find the following chunk of code (line 4704):

############## TEXT AND html MODE ##############


  .split.line <-    # split line of a LaTeX table into constituent parts separated by &
  function(s) {
    # remove the "\\\\"
    s <- gsub("\\\\", "", s, fixed=TRUE)
    s <- paste("  ",s,"  ", sep="")

    return(.trim(strsplit(s, " &", fixed=TRUE)[[1]]))
  }

So, apparently, this seems to be hardcoded into the way stargazer is formatting the table output and is not captured by any option to the command.

If you simply want to format a data frame similar to the one you posted in your example, I would use print.xtable() from the xtable package which has a sanitize function that correctly handles ampersands in strings and produces both correct LaTeX and HTML outupt for the suggested example data frame.

Bron answered 18/4, 2015 at 6:59 Comment(3)
you could also try Hmisc::latex().Hoarsen
(+1) Thanks for the answer... Im curious though .split.line seems to ignore \\& but stargazer does not. For example, z <- c("JPMorgan Chase \\& Co", "Wells Fargo & Co"); lapply(z, .split.line) ; s <- data.frame(z=z, l= c(100000, 25)) ; stargazer(s, type = "text", summary=F). Sorry im probably missing the obvious.Boru
ah.. there are also other functions that replace characters , .replace.latex.symbols and so on...Boru

© 2022 - 2024 — McMap. All rights reserved.