2
> df = data.frame(Parameters = c(expression(beta[1])))
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ""expression"" to a data.frame

I'm trying to write math notation in a data.frame, but it seems that the two are not compatible. Is there a way around this?

I have also tried

> data.frame(Parameters = paste(expression(beta[1])))
  Parameters
1    beta[1]

How can I get enter image description here to show up?

Adrian
  • 9,229
  • 24
  • 74
  • 132
  • 3
    Why are you trying to do this? What is your actual goal? – Dason Sep 19 '17 at 16:18
  • You can store strings in a data frame – John Coleman Sep 19 '17 at 16:21
  • @Dason I'm trying to store the string $\beta_1$ into my data.frame. Instead of writing just "beta1" I want it as the actual mathematical notation, i.e. expression(beta[1]) – Adrian Sep 19 '17 at 17:03
  • So you're trying to use `?plotmath` syntax in a data.frame column? That's not really going to work; that only works when plotting. Not sure where exactly you want this to render. Is this for a plot of some kind? – MrFlick Sep 19 '17 at 17:06
  • I see. I just wanted to create data.frame that I will pass into xtable so the final result will be in LaTeX. Just wanted the mathematical notations to come out correctly instead of just "beta1", "hat_sigma" or something of that sort. – Adrian Sep 19 '17 at 17:09
  • 4
    Perhaps you could just use Unicode for beta. For actual LaTex, perhaps you can use RMarkdown and LaTex for inserting the dataframe in a document with the symbols that you want. I don't think that you can directly display LaTex in a column of a dataframe in the R Console. – John Coleman Sep 19 '17 at 17:17
  • 1
    Perhaps you're after something like this: https://stat.ethz.ch/pipermail/r-help/2007-April/130864.html – MrFlick Sep 19 '17 at 18:46
  • 1
    Expanding on @John Coleman's suggestion of using unicode, [this post](https://stackoverflow.com/questions/48877641/superscripts-in-r) provides examples of how to do this with data frames in R. – SnowFrog Aug 09 '18 at 11:55

3 Answers3

1

If you want to store the latex code for those symbols inside a dataframe then be able to generate correct latex code from xtable, you will need to override the sanitize function in print.xtable by feeding in a dummy function that returns the input exactly (See this question: Using xtable with R and Latex, math mode in column names?):

df = data.frame(Parameter = c("$\\beta_{0}$", "$\\beta_{1}$", "$\\beta_{2}$"),
                Estimate = beta, row.names = 1)

print(xtable(t(df)), sanitize.text.function = function(x){x})

Latex Table:

\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
  \hline
 & $\beta_{0}$ & $\beta_{1}$ & $\beta_{2}$ \\ 
  \hline
Estimate & 0.05 & 0.10 & 0.15 \\ 
   \hline
\end{tabular}
\end{table}

Similar to xtable, stargazer has some cool options to generate nice looking tables in latex. One thing you can do is to change the variable names to math notation using the covariate.labels argument in stargazer:

library(stargazer)

beta = 1:3*0.05
df = data.frame(Parameter = c("beta0", "beta1", "beta2"),
                Estimate = beta, row.names = 1)

stargazer(t(df), covariate.labels = c(NA, "$\\beta_{0}$", "$\\beta_{1}$", "$\\beta_{2}$"),
          header = FALSE, summary = FALSE)

This outputs a latex table code:

\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & $\beta_{0}$ & $\beta_{1}$ & $\beta_{2}$ \\ 
\hline \\[-1.8ex] 
Estimate & $0.050$ & $0.100$ & $0.150$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 

You can copy and paste the code here to render the latex table.

enter image description here

Also note that the default for type= in stargazer is "latex", which generates latex code, but you can also specify type="text" to generate a table in your console. This option, however, does not allow you to render the math symbols.

stargazer(t(df), covariate.labels = c(NA, "$\\beta_{0}$", "$\\beta_{1}$", "$\\beta_{2}$"),
          header = FALSE, summary = FALSE, type = "text")

# ==========================
#              0     1     2  
# --------------------------
# Estimate 0.050 0.100 0.150
# --------------------------
acylam
  • 18,231
  • 5
  • 36
  • 45
1

Another option using my package:

library(huxtable)
dfr = data.frame(Parameter = c("$\\beta_{0}$", "$\\beta_{1}$", "$\\beta_{2}$"),
      Estimate = 'beta')
ht <- as_hux(dfr)
escape_contents(ht) <- FALSE
ht # will print as TeX within a markdown pdf_document
-3

I am not very sure what you are trying to do here. If you are trying to create a dataframe df with a column named "Parameter" with values taken from a vector within a list beta, then the below code will do the job.

df = data.frame(Parameters = beta[[1]]) 
# Assuming that the first object in beta is a vector that you want to set as "Paramters" column.

Please provide more information as to what these objects are if this is not what you were looking for.

Sujeet Pillai
  • 37
  • 1
  • 6