I having a rough time figuring out how to superscript and subscripts text in my plots and tables in R. I just want to superscript the "-1" in the row "Units" below:
# Data for a table. I want the "-1" in the units row superscripted.
df <- data.frame("X"=c("Unit", "Mean", "CV"),
"Total C" = c("g kg-1", 8.16, 5.94),
"Total N" = c("g kg-1", 0.51, 7.97))
The base function expression
doesn't seem to work:
# I initially thought I would use a loop.
text <- matrix(df[1,])[1]
prefix <- substr(text, 1, nchar(text)-2)
suffix <- substr(text, nchar(text)-1, nchar(text))
# new <- expression(prefix ^ suffix)
# Doesn't work. Literally returns "expression(prefix^suffix)".
I thought I would use KableExtra
to create the table. There appears to be a function suited for manipulating text (text_spec
), but I can't find any resources on how to use it to subscript and superscript text.
This Stack Overflow thread seems to show how to use LaTex styling in a related Kable Extra function group_rows
, but I can't figure out how to use it for my intended purpose.
Any help would be appreciated. Thank you.
Limited usability, depending on how you'll be using the Units
column but this works:
df <- data.frame("X"=c("Unit", "Mean", "CV"),
"Total C" = c(paste0("g kg", footnote_marker_number(-1, "latex")), 8.16, 5.94),
"Total N" = c(paste0("g kg", footnote_marker_number(-1, "latex")), 0.51, 7.97))
kable(df, format = "latex", escape = F)