0

How can we print H₂O in R? And, I am not looking for labels sub/superscripted in plots. I want to run:

cat("H2O")

And see H₂O

robinhoodjr
  • 415
  • 8
  • 20
  • I think we should try `var=expression(paste(H[2], O))` for subscript or `var=expression(paste(H^2, O))` for superscript, check this [Link](https://stackoverflow.com/questions/10156417/subscripts-in-plots-in-r) – Adji Jan 15 '20 at 03:36

1 Answers1

1

Using a one-liner with the UTF-8 character for subscript 2:

paste0("H", "\u2082", "O")
[1] "H₂O"

The UTF-8 character for subscript two as an R string literal is "\u2082.

Note that if your R console/tool be already UTF-8 enabled, then you should simply be able to use a literal string, e.g.

"H₂O"
[1] "H₂O"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360