 # 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")
library(tidyverse)
if_else(marklist>=85, "B+",if_else(marklist>=75, "C",
if_else(marklist>=50, "D","F")))))
``````

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

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

``````marklist<- c(98,74,32,58,64,98,85,51)
names<- c("A","B","C","D","E","F","G","H")
library(tidyverse)
if_else(marklist>=85, expression("B"^"+") ,if_else(marklist>=75, expression( "C"),
if_else(marklist>=50, expression("D"),expression("F"))))))
``````

here is the error

• 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.
``````marklist<- c(98,74,32,58,64,98,85,51)
names<- c("A","B","C","D","E","F","G","H")
library(tidyverse)

case_when(marklist >= 90~ "A",
marklist >= 85~ '"B"^"+"',
marklist >= 75~ "C",
marklist >= 50~ "D",
TRUE ~ "F")) %>% arrange(marklist)

``````

1 Like

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.

``````library(readr)
library(tidyverse)
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

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

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"))
)))
``````

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