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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With