Superscript in character string

Hi,

I am trying to create a chatacter string '6th EP' with 'th' as a superscript, any advice?

Many thanks,

Jakub

You need to use the ^ operator before the superscript. I presume you want to use this character string in a plot or Markdown so here's an example where the label of the X-axis contains a superscript.

plot(rnorm(10), xlab = expression(paste("6"^"th", " EP")))

Created on 2020-05-04 by the reprex package (v0.3.0)

I am just creating a dataframe. Shall I use c(expression(paste("6"^"th", " EP"))) ?

You mean you want to use this in a variable name? That's not really advisable.

I am creating a dataframe which I want to use for the creation of png table.

Sorry, but I still don't follow. Could you please provide a more detailed explanation?

This is my code.

library(knitr)
library(kableExtra)
SEATS<- data.frame(matrix(ncol = 3, nrow = 10))
rownames(SEATS) <- c("European People's Party group (EPP)", 
                     "Progressive Alliance of Socialists and Democrats (S&D)",
                     "European Conservatives and Reformists (ECR)",
                     "Group of the Alliance of Liberals and Democrats for Europe (ALDE)",
                     "European United Left/Nordic Green Left (GUE/NGL)",
                     "The Greens/European Free Alliance (Greens/EFA)",
                     "Europe of Freedom and Direct Democracy (EFDD)",
                     "Europe of Nations and Freedom (ENF)",
                     "Union for Europe of the Nations (UEN)",
                     "Independence/Democracy (IND/DEM)")



colnames(SEATS) <-c("6th EP","7th EP","8th EP")

SEATS[1,] <- c(288,273,216)
SEATS[2,] <- c(218,195,185)
SEATS[3,] <- c(0,57,77)
SEATS[4,] <- c(100,83,69)
SEATS[5,] <- c(40,35,52)
SEATS[6,] <- c(43,57,52)
SEATS[7,] <- c(0,31,42)
SEATS[8,] <- c(0,0,36)
SEATS[9,] <- c(44,0,0)
SEATS[10,] <- c(22,0,0)

SEATSl <- kable(SEATS, format="latex")
save_kable(SEATSl, file="SEATS.png")

I am trying to create a table.

OK, so you are trying to name your data frame's variables. I don't think such characters are allowed in data frame column names (although I could be wrong).

The proper approach to your situation (in my not so gentle opinion) is not to name your data frame with a superscript, but to set the column name when you write your table.

In other words, avoid pre-processing your data into your output as much as possible.

The new gt package might be of interest (I've not tried using it yet, but from what I have read, it supports this approach). huxtable, and pixiedust are options as well (disclosure, I'm the author of pixiedust)

To approach this in pixiedust, I would try (untested code)

library(pixiedust)
dust(SEATS) %>%
  sprinkle_colnames(ep6 = "6^{th} EP", 
                    ep7 = "7^th EP") %>%
  sprinkle_print_method("latex")

Is it possible that the names of columns of dataframe have a supercript?

I'm not aware of any meaningful or generalized way of doing this.

How do you suggest I should convert latex to png? Thank you!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.