“有条件的”地块
我很好奇如何绘制这样定义的函数:
if(x < 1)
f(x) = x/10 * 1.2
if(x < 3)
f(x) = x/12 * 1.7
...
else
f(x) = x/15 * 2
如果函数很简单,例如 f(x) = x/10 * x/5 ,那么就不会有问题,可以使用 curve() 方法。但是我不确定处理更复杂的函数(如上面的函数)的最佳方法是什么。有什么想法吗?如果可以使用 ggplot() 的话,加分:)
I am curious how to plot function that is defined something like this:
if(x < 1)
f(x) = x/10 * 1.2
if(x < 3)
f(x) = x/12 * 1.7
...
else
f(x) = x/15 * 2
If the function was simple, say f(x) = x/10 * x/5 , then there would be no problem, and one could use curve() method. However I am not sure what is the best way to deal with more complex functions, like the one above. Any ideas? Bonus points, if ggplot() could be used :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
曲线还是有可能的。 (当您阅读统计文献时,这个公式显示为 I[x],“I”代表“指标”。)
Curve is still a possibility. (And as you read the statistical literature, this formulation shows up as I[x], "I" being for "indicator".)
您在寻找类似
stepfun
的东西吗?指定函数的方式会有点不同,但一旦掌握了就很容易掌握阅读
?stepfun
并自己绘制一些示例。Are you looking for something like
stepfun
?The way you specify the function will be a bit different, but it's fairly easy to grasp once you've read
?stepfun
and plotted some examples yourself.口味问题,但我更喜欢
ifelse
而不是 Dwins 指标(例如mbq 评论)。用于比较:Matter of taste but I prefer
ifelse
over Dwins indicators (like in mbq comment). For compare: