Superscript and Subscript character in R

How we can assign superscript and subscript in R. for example

marklist<- c(98,74,32,58,64,98,85,51)
names<- c("A","B","C","D","E","F","G","H")
grade<-data.frame(marklist,names)
library(tidyverse)
grade <- grade %>% mutate(grade= if_else(marklist>=90, "A",
if_else(marklist>=85, "B+",if_else(marklist>=75, "C", 
if_else(marklist>=50, "D","F")))))

print(grade)
grade
marklist names grade
1 98 A A
2 74 B D
3 32 C F
4 58 D D
5 64 E D
6 98 F A
7 85 G B+
8 51 H D
my question is how I can write B plus superscript as a character " B +" plus sign should be superscript.

This will produce subscripts:

expression("A"^"+")

and this will produce subscripts:

expression("A"['+'])

These sub- and superscripts are only visible in plots and possibly, formatted tables produced by rmarkdown.

1 Like
library(readr)
demo_g <- read_csv("demo_g.csv")
library(tidyverse)
grade <- demo_g %>% mutate(grade= if_else(Marklist>=90, "A",
if_else(Marklist>=85, expression("B"^"+"),if_else(Marklist>=75, "C", 
if_else(Marklist>=50, "D","F")))))

Error: Problem with mutate() input grade.
x false must be an expression vector, not a character vector.
i Input grade is if_else(...).
it is not valid for character

From the error messages, I think you need to wrap the aspects of the ifelse statement in an expression() statement.

Could you show me how to wrap

For so many conditions to evaluate case_when is probably better than if_else

Can you provide an example to illustrate how case_when() is better?

This may work:

demo_g$grade <- if_else(Marklist>=90, expression("A"),
     if_else(Marklist>=85, expression("B"^"+"),
          if_else(Marklist>=75, expression("C"), 
             if_else(Marklist>=50,  expression("D"),expression("F"))
     )))
marklist<- c(98,74,32,58,64,98,85,51)
names<- c("A","B","C","D","E","F","G","H")
grade<-data.frame(marklist,names)
library(tidyverse)
grade <- grade %>% mutate(grade= if_else(marklist>=90, expression("A"),
if_else(marklist>=85, expression("B"^"+") ,if_else(marklist>=75, expression( "C"), 
if_else(marklist>=50, expression("D"),expression("F"))))))

here is the error

grade <- grade %>% mutate(grade= if_else(marklist>=90, expression("A"),

  • if_else(marklist>=85, expression("B"^"+") ,if_else(marklist>=75, expression( "C"),
  • if_else(marklist>=50, expression("D"),expression("F"))))))
    Error: Problem with mutate() input grade.
    x Input grade must be a vector, not an expression vector.
    i Input grade is if_else(...).
    Run rlang::last_error() to see where the error occurred.
1 Like
marklist<- c(98,74,32,58,64,98,85,51)
names<- c("A","B","C","D","E","F","G","H")
grade_df<-data.frame(marklist,names)
library(tidyverse)

grade_df <- grade_df %>% 
  mutate(grade=
           case_when(marklist >= 90~ "A",
                     marklist >= 85~ '"B"^"+"',
                     marklist >= 75~ "C",
                     marklist >= 50~ "D",
                     TRUE ~ "F")) %>% arrange(marklist)
 
grade_labels <-  parse(text=unique(grade_df$grade))

ggplot(data=grade_df) +
  geom_bar(mapping=aes(grade)) +
  scale_x_discrete(labels= grade_labels)

print(grade_df)
marklist names grade
1 32 C F
2 51 H D
3 58 D D
4 64 E D
5 74 B D
6 85 G "B"^"+"
7 98 A A
8 98 F A
it does not work for data frame or table, for a plot it works, my question is how we can write superscript and subscript in a table, data-frame.

You should not expect it to work in a data.frame.
It is purely representational therefore when it is time to publish/print a nice table for presentation you would pick one of the many table printing packages... gt, flextable,datatable, huxtable, etc etc.

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