1

This is more for curiosity, but is it possible to write subscripts in R console and, if so, how would I do that?

  • I am using a Mac and
  • typically code in RStudio

Although assignment statements work with Greek letters, e.g. μ,σ, but I can't get superscripts or subscripts working. I'd like to write an assign statement such as σ² <- 1 and have it be recognized. In particular, typing that last command I get the following error message

Error: unexpected input in "σ�"
clarity123
  • 1,956
  • 10
  • 16
lobgoblin
  • 75
  • 7

2 Answers2

1

You can, but you shouldn't; names that don't stick to R's standards cause problems eventually.

The rules, from ?Quotes:

Names and Identifiers

Identifiers consist of a sequence of letters, digits, the period (.) and the underscore. They must not start with a digit nor underscore, nor with a period followed by a digit. Reserved words are not valid identifiers.

The definition of a letter depends on the current locale, but only ASCII digits are considered to be digits.

Such identifiers are also known as syntactic names and may be used directly in R code. Almost always, other names can be used provided they are quoted. The preferred quote is the backtick (`), and deparse will normally use it, but under many circumstances single or double quotes can be used (as a character constant will often be converted to a name). One place where backticks may be essential is to delimit variable names in formulae: see formula.

If you still want to break the rules, there are a couple ways. You can use assign:

> assign('σ²', 47)
> `σ²`
[1] 47
> σ²
Error: unexpected input in "σ�"

Notice, however, that dependent on your locale, you may need to wrap σ² in backticks to call it successfully.

You can also wrap it in backticks to assign:

> `σ²` <- 47
> `σ²`
[1] 47
> σ²
Error: unexpected input in "σ�"

As you can see, you'll still likely need to wrap any calls in backticks (especially if you want your code to be faintly portable).

That said, this is all a REALLY, REALLY BAD IDEA.

  1. Your variable names will be hard to type;
  2. Your code will likely not be portable;
  3. You stand a very good chance of causing errors in more complicated functions later (e.g. NSE functions would be very unpredictable);
  4. It's a good way to do really stupid things:

like

> `+` <- `-`
> 1 + 1
[1] 0

The rules are there for a reason. Unless you've got a really good reason to break them, don't.


Addendum

If you just want to use symbolic expressions as strings, encodeString and format are both useful:

> encodeString('σ²')
[1] "σ²"
> format('σ²')
[1] "σ²"

See their help pages for more specifics, but it's generally possible to use any symbol (even emoji!) in a string, if you're a little careful what functions you pass it to.

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • Neither (3) nor (4) are convincing to me: (3), I think, is simply wrong: NSE treats these variables like any others. The only problem could occur if the NSE function calls `deparse` incorrectly (for backwards compatibility reasons, its argument `backtick` defaults to `FALSE` for most expressions, but it *must* be set to `TRUE`). (4) … I don’t really see what this has to do with Unicode variable names. You can redefine operators and other functions anyway. – Konrad Rudolph Feb 15 '16 at 00:28
  • @KonradRudolph For (4), sure, you need to be smart enough not to do such things. For (3), though, it may not be NSE (which can be implemented differently), but even `data.frame` normalizes the name `σ²` unpredictably to `σ.`, which if you didn't look (unlikely, granted) would cause downstream errors. – alistaire Feb 15 '16 at 00:37
  • 2
    You can fix `data.frame`’s behaviour by specifying `check.names = FALSE`. People generally prefer that the default behaviour of `data.frame` is bad, which is why modern replacements, such as `dplyr::data_frame`, do it differently. You’re right though that caution is necessary. – Konrad Rudolph Feb 15 '16 at 09:45
0

I am using windows so maybe this doesn't work on a mac. The following code allows superscript for me.

    > assign("σ²", 3)
    > σ²
    [1] 3

Hope this helps :)

Vandenman
  • 3,046
  • 20
  • 33