Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: how to construct a mathematical expression from a character object?

Tags:

r

In R, I'm wanting to create a graph with x axis label expression(varname), where varname is a character object. For example:

varname <- "beta[1]"
hist(rnorm(20),xlab=expression(varname))

But that gives me a graph with xlab="varname" rather than xlab=expression(beta[1]). How to I convince expression() to evaluate the variable?

like image 598
Rob Hyndman Avatar asked Jan 01 '26 09:01

Rob Hyndman


1 Answers

you can do by

hist(rnorm(20),xlab=parse(text=varname))
like image 56
kohske Avatar answered Jan 04 '26 05:01

kohske



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!