Jay
Jay

Reputation: 2141

Use greek letters to name list elements in R

Is there any way to name list elements in R using Greek letters? I'm asked to create a list that should look like list(α = 42). The actual result of this expression is equivalent to the result of list(a = 42).

I know it is possible to use Greek letters in plots using expression(symbol("a")), but I couldn't find a solution to use Greek letters as names for list elements. Using as.character("\U03B1") results in an error, using just "\U03B1" leads to an "a". I doubt that it makes sense to use Greek letters anywhere else than plots, but this is homework, so I have to find a way (if there is one).

Upvotes: 2

Views: 2880

Answers (2)

Simon O'Hanlon
Simon O'Hanlon

Reputation: 59970

Alternatively, name the list afterwards using your unicode symbols in the character vector:

ll <- as.list( 42 )
names(ll) <- "\u03B1"
ll
#$α
#[1] 42

Interesting that you need to do this. Also your list holds the meaning of life.

Upvotes: 2

nico
nico

Reputation: 51640

I have not tested this thoroughly, but R seems to take pretty much any symbol as a variable name without any problems and I could not find any specific rule about variable naming aside from this, taken from ?name

Names are limited to 10,000 bytes (and were to 256 bytes in versions of R before 2.13.0).

The code you posted works perfectly for me (R 3.0.1 running under Fedora Core 18):

> a <- list(α = 42)
> a
$α
[1] 42

That said, I would definitely suggest to just spell out the letter as "alpha", as it is more practical to write and maintain.

a <- list(alpha = 42)

Upvotes: 4

Related Questions