David Z David Z - 3 months ago 12
R Question

How to index Greek symbols in R expression

Here is a simple example:

ylab<-expression(paste(beta, "/ml"))
plot(1:10, ylab=ylab)


enter image description here

but if I index beta using another variable:

unit<-beta
ylab<-expression(paste(unit, "/ml"))
plot(1:10, ylab=ylab)


enter image description here

My question is how to index these Greek symbols in R expression?

Answer

first off beta should be double-quoted so it's a string not the base function, as pointed out by @42-

otherwise it seems you can paste a string together and then evaluate it like this

unit<-"beta" #note double quotes
txt <- paste0("expression(",unit,"~'/ml')") #built expression as string
ylab<-eval(parse(text=txt)) #evaluate the expression with parse
plot(1:10, ylab=ylab)

enter image description here

Comments