Including variables in expression call in R

蓝咒 提交于 2020-01-03 02:47:05

问题


I was wondering if it were possible to include variables when using expression in R.

For instance I would like to do something like this:

par(mfrow=c(2,3))
for (i in 1:6)
    {
    plot(x, p1-i*p2, main=expression(Phi[1] - i * Phi[2]))
    }

But this does not work, as it prints Φ1 - iΦ2 (i.e. it does not substitute i with 1, 2, ... 6


回答1:


Use substitute:

> substitute(Phi[1] - i* Phi[2], list(i = i))
Phi[1] - 3 * Phi[2]



回答2:


Looks like it uses the variables. I set x = 1 and used the following.

> p1 = 100
> p2 = 10
> for (i in 1:6)
+     {
+     plot(x, p1-i*p2, main=expression(Phi[1] - i * Phi[2]))
+     }
> 

It gave me 6 graphs for Φ1 - iΦ2 with each showing the Y value descending.



来源:https://stackoverflow.com/questions/3345625/including-variables-in-expression-call-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!