# Insert specific unicode symbols inside values of data.frame variable

In my data.frame I would like to add two variables, "A" and "B", whose values contain respectively an n with the i subscript and an n with the s subscript.
As I have understood so far, it's not possible to specify an expression for the values of a variable, and hence to add special characters it's necessary to use unicode symbols. Some of this unicodes work in R, as for example the greek letter "mu", identified with the unicode \U00B5, or numeric subscripts, as you can see in this reprex in your R console:

x <- data.frame("A" = c("\U00B5"),
"B" = c("B\U2082"))

print(x)


These unicodes work also if I decide to put this variable in a ggplot() object, because I will display the correct symbol ("mu" for example) on the axis text or the facets.
The problem is that when I do the same for the subscripts of i (unicode: \U1D62) and s (unicode: \u209B), R doesn't recognise the unicode and prints the whole string inside the variable name.

Do you know how I can resolve this issue and if this unicode works on every operating system?

Thanks

Please see the policy on cross posts

xsubi <- "X\u1d62"
xsubi
#> [1] "X\u1d62"
cat(xsubi)
#> Xᵢ
xsubs <- "X\u209b"
xsubs
#> [1] "X\u209b"
cat(xsubs)
#> Xₛ


Created on 2019-11-29 by the reprex package (v0.3.0)

The differences are caused by which code points are supported by which encoding and whether the font has the code point. Glyphs for superscripts and subscripts are problematic, and no compelling reason comes to mind for preferring them over mark-up language representations.

The alternative is to enter the variables as \LaTeX markup

x <- data.frame("mu"    = "$\\mu$",
"bsub2" = "$B_2$",
"xsubi" = "$X_i$",
"xsubs" = "$X_s$")
x
#>       mu bsub2 xsubi xsubs
#> 1 $\\mu$ $B_2$ $X_i$ $X_s$


Created on 2019-11-29 by the reprex package (v0.3.0)

To display, save an Rmd file with those contents and apply rmarkdown::render, as shown here. Not that I'd recommend putting mark-up language symbols as data frame entries.

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