【发布时间】:2015-03-01 15:25:27
【问题描述】:
以下代码如何工作?我在阅读 R ?curve 的帮助行时得到了这个例子。但是这个我没看懂。
for(ll in c("", "x", "y", "xy"))
curve(log(1+x), 1, 100, log = ll,
sub = paste("log= '", ll, "'", sep = ""))
特别是,我习惯于将数值作为for-loop 中的参数作为,
for(ll in 1:10)
但是下面的命令是什么意思:
for(ll in c("","x","y","xy"))
c("","x","y","xy") 看起来像一个字符串向量? c("","x","y","xy") 如何在 curve 内部工作
作为log(1+x)[这里的x是什么?字符串“x”?在c("","x","y","xy")] 和log=ll 中?
【问题讨论】:
-
曲线中的
log(1+x)部分不受循环的直接影响。如您所见,ll遍历字符向量c("","x","y","xy")并提供给curve内的log参数。参数说明为:The value of log is used both to specify the plot axes (unless add = TRUE) and how ‘equally spaced’ is interpreted: if the x component indicates log-scaling, the points at which the expression or function is plotted are equally spaced on log scale.所以如果你定义比如log = "x"表示你的x轴将以对数显示。 -
简短的回答是参数
ll仅被传递给curve中的形式参数,即“期望”(或更准确地定义为接收)字符值。 -
for (letter in c("a", "b", "c")) print(letter)